Reputation: 91
I am working with WordPress 3.9 and Woocommerce in templates/../my-account.php
. I did not like the default my-account page and wanted the user to be able to edit specific settings so I set it up so the user can change their username, email, or password. That was the goal at least haha.
In the debugging process, I found the problem was with $user_id = wp_update_user( $userdata );
. is_wp_error($user_id)
returns false
. When I use wp_get_current_user();
I am able to echo
user_email
, for example, and the user's email is displayed. I'm pretty sure I created the $userdata
array correctly, so what do I need to change to be able to update the user's settings?
I could be making a mistake, but to my knowledge I'm doing everything according to the codex docs.
Here's my code for my-account.php if you need other code or more information I can supply. Thank you for any help.
<?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(' download remaining', ' 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');
Upvotes: 0
Views: 1957
Reputation: 63
I believe you need to include the user's ID in your $userdata array. For instance:
$userdata = array(
'ID' => $current_user->ID,
'user_email' => $em
);
Upvotes: 1