Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WP WP_update_user($userdata)错误_Php_Wordpress_Woocommerce - Fatal编程技术网

Php WP WP_update_user($userdata)错误

Php WP WP_update_user($userdata)错误,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在使用WordPress 3.9和Woocommerce的模板/./my account.php。我不喜欢默认的“我的帐户”页面,希望用户能够编辑特定的设置,因此我将其设置为用户可以更改其用户名、电子邮件或密码。至少这是目标,哈哈 在调试过程中,我发现问题出在$user\u id=wp\u update\u user($userdata)是错误($user\u id)返回false。当我使用wp_get_current_user()时例如,我可以回显用户的电子邮件,并显示用户的电子邮件。我很

我正在使用WordPress 3.9和Woocommerce的
模板/./my account.php
。我不喜欢默认的“我的帐户”页面,希望用户能够编辑特定的设置,因此我将其设置为用户可以更改其用户名、电子邮件或密码。至少这是目标,哈哈

在调试过程中,我发现问题出在
$user\u id=wp\u update\u user($userdata)<代码>是错误($user\u id)
返回
false
。当我使用
wp_get_current_user()时例如,我可以
回显
用户的电子邮件
,并显示用户的电子邮件。我很确定我正确地创建了
$userdata
数组,那么我需要更改什么才能更新用户的设置呢

我可能犯了一个错误,但据我所知,我做的每件事都是按计划进行的

如果您需要其他代码或我可以提供的更多信息,请参阅我的my-account.php代码。谢谢你的帮助

<?php
/**
 * My Account
 */

global $woocommerce;
global $current_user;
?>
<style type="text/css">
    #edit-account-settings { font-size: 1.2em; width: 280px; }
    #edit-account-settings input { width: 100%; }
    #edit-submit { font-size: 1em; }
</style>
<?php

wp_get_current_user();
if(is_null($current_user)) {
    echo 'Sorry, you are not logged in. '.wp_loginout();
} else {
    $php_option = false;
    $fName = $current_user->first_name;
    $lName = $current_user->last_name;
    $username = $current_user->user_login;
    $email = $current_user->user_email;
    print '<div id="account-settings"><h2>Welcome '.$fName.' '.$lName.'</h2>';
    print '<p style="font-size: 1.4em;">User Name: '.$username.'</p>';
    print '<p style="font-size: 1.4em;">Email: '.$email.'</p>';
    print '<p style="font-size: 1.4em;">Password: ********</p>';
    print '<a id="edit-account-settings" href="#" onclick="edit_settings();">Change your account settings</a></div>';
}
?>

<script>
    function edit_settings() {
        //alert("inside onclick event");
        var username = <?php echo json_encode($username); ?>;
        var email = <?php echo json_encode($email); ?>;
        var data = '<p style="font-size: 1.2em;">To change your settings, enter the new value in the appropriate text field</p>';
        data += '<p style="font-size: 1.2em;">If do not want to change your password, enter your current password twice.</p>';
        data += '<p style="font-size: 1.2em;">If you want your other settings to remain the same, leave the fields as they are and submit.</p>';
        data += '<form id="edit-account-settings" name="edit-account-settings" method="post" action="">';
        data += '<label for="un">User Name: </label><input type="text" id="un" name="un" value="'+username+'" maxlength="16" required />';
        data += '<label for="email">Email: </label><input type="email" id="em" name="em" value="'+email+'" maxlength="32" required />';
        data += '<br /><br /><input type="password" id="p1" name="p1" value="" placeholder="Enter a new password" maxlength="16" required />';
        data += '<br /><br /><input type="password" id="p2" name="p2" value="" placeholder="Re-enter password" maxlength="16" required />';
        data += '<br /><br /><input type="submit" id="edit-submit" name="edit-submit" value="Submit Changes" />';
        data += '</form></div>';
        var elem = document.getElementById("account-settings");
        elem.innerHTML = data;
    }
</script>

<?php
    if (isset($_POST['em']) && $_POST['em'] != $email && $_POST['em'] != "") {
        $em = $_POST['em'];
        $userdata = array(
            'user_email' => $em
        );

        if (!empty($userdata) || isset($userdata['user_email'])) {
            $user_id = wp_update_user( $userdata );
            if(is_wp_error($user_id)) {
                echo "<p style='color: #FF0000;'>Error: Sorry your settings could not be updated. Please try again in a minute.</p>";
            } else {
                echo "<p style='color: #00FF00;'>Congradulations! Your settings have been updated.</p>";
            }
        } else {
            echo "<p>$userdata is null or undefined</p>";
        }
    } else {
        echo "<p>Your email address is the same</p>";
    }
?>

<?php do_action('woocommerce_before_my_account'); ?>

<?php if ($downloads = $woocommerce->customer->get_downloadable_products()) : ?>
<h2><?php _e('Available downloads', 'woocommerce'); ?></h2>
<ul class="digital-downloads">
    <?php foreach ($downloads as $download) : ?>
        <li><?php if (is_numeric($download['downloads_remaining'])) : ?><span class="count"><?php echo $download['downloads_remaining'] . _n('&nbsp;download remaining', '&nbsp;downloads remaining', $download['downloads_remaining'], 'woocommerce'); ?></span><?php endif; ?> <a href="<?php echo esc_url( $download['download_url'] ); ?>"><?php echo $download['download_name']; ?></a></li>
    <?php endforeach; ?>
</ul>
<?php endif; ?> 

<h2><?php _e('Recent Orders', 'woocommerce'); ?></h2>
<?php woocommerce_get_template('myaccount/my-orders.php', array( 'recent_orders' => $recent_orders )); ?>

<h2><?php _e('My Address', 'woocommerce'); ?></h2>  
<p class="myaccount_address"><?php _e('The following addresses will be used on the checkout page by default.', 'woocommerce'); ?></p>
<?php woocommerce_get_template('myaccount/my-address.php'); ?>

<?php
do_action('woocommerce_after_my_account');

#编辑帐户设置{字体大小:1.2em;宽度:280px;}
#编辑帐户设置输入{宽度:100%;}
#编辑提交{字体大小:1em;}
函数编辑_设置(){
//警报(“内部onclick事件”);
var用户名=;
var电子邮件=;
var data='

要更改设置,请在相应的文本字段中输入新值; data+='

如果不想更改密码,请输入当前密码两次。

'; 数据+='

如果希望其他设置保持不变,请保持字段不变并提交。

'; 数据+=''; 数据+='用户名:'; 数据+='电子邮件:'; 数据+='

'; 数据+='

'; 数据+='

'; 数据+=''; var elem=document.getElementById(“帐户设置”); elem.innerHTML=数据; }
我认为您需要在$userdata数组中包含用户ID。例如:

    $userdata = array(
        'ID' => $current_user->ID,
        'user_email' => $em
    );

太棒了。我真不敢相信事情会这么简单。。。谢谢你,贾斯汀。