WP wp_update_user($ userdata)错误

时间:2014-08-12 22:19:18

标签: php wordpress woocommerce

我正在使用templates/../my-account.php中的WordPress 3.9和Woocommerce。我不喜欢默认的my-account页面,并希望用户能够编辑特定设置,因此我进行了设置,以便用户可以更改其用户名,电子邮件或密码。那个目标至少是哈哈。

在调试过程中,我发现问题出在$user_id = wp_update_user( $userdata );is_wp_error($user_id)返回false。当我使用wp_get_current_user();时,我可以echo user_email,例如,系统会显示用户的电子邮件。我确定我已正确创建$userdata数组,那么我需要更改哪些内容才能更新用户的设置?

我可能犯了一个错误,但据我所知,我根据codex docs做了一切。

如果您需要其他代码或我可以提供的更多信息,请访问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 个答案:

答案 0 :(得分:1)

我认为您需要在$ userdata数组中包含用户ID。例如:

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