Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created user meta options using this tutorial. Now I want to know how to allow users to edit (update) certain custom user meta options that I created.

Users already have a template account page where they have access to some user meta options. I added the a function to the users' page to update the meta field upon clicking the update button on the page.

Im using text input fields for the new user metas. This is the code I used to update the options:

add_action( 'personal_options_update', 'my_save_extra_user_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_user_fields' );

function my_save_extra_user_fields( $userId ) {

    if ( !current_user_can( 'edit_user', $userId ) )
        return false;

    update_usermeta( $userId, 'address2', $_POST['address2'] ); // Custom meta 1
    update_usermeta( $userId, 'address3', $_POST['address3'] ); // Custom meta 2
    update_usermeta( $userId, 'hours', $_POST['hours'] ); // Custom meta 3
}

But when I update the page it shows the previous values saved in the back-end (admin) section of wordpress. Of course, any values saved in the back end are saved correctly and displayed on the front-end. I wanted to save either way, from back-end and from front-end.

What am I doing wrong? How do I fix this?

Thanks

UPDATE:

Here is the code for the template page that users use to update their existing meta information:

<?php get_header(); ?>

<script src='../js/states-n-cities.js'></script>


<?php 

$user_info = get_userdata($current_user->ID); 
$userId = $userdata->ID;

$location = $userdata->jabber;
$lsplit = explode('**',$location);
$full_location =  ''.$lsplit[2]. ', ' .$lsplit[3]. ' ' .$lsplit[1]. ' ' .$lsplit[4]. ''; // 0 = Country; 1 = State; 2 = Address; 3 = City; 4 = Zip Code; 5 = Phone Number
$statesplit = explode(', ', $lsplit[1]); // State split: 0 = State name; 1 = State Abbreviation
$location_pt1 =  $lsplit[2];
$location_pt2 =  ''.$lsplit[3]. ', ' .$statesplit[1]. ' ' .$lsplit[4]. '';

?>


<?php echo $PPTDesign->GL_ALERT($GLOBALS['error_msg'],$GLOBALS['error_type']); ?>

<div id="My" style="display:visible;">

<h1 class="page-title">My Account</h1>

<div class="middleSidebar left padding-form" style="width: 710px;">


<div class="fieldset">

    <div class="full clearfix border_t box"> <br />
    <p class="f_half left"> 
        <a href="javascript:void(0);" onClick="jQuery('#My').hide(); jQuery('#MyDetails').show()" title="<?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount2']) ?>">
        <img src="<?php echo IMAGE_PATH; ?>a1.png" style="float:left; padding-right:20px; margin-top:10px;" />
        <b><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount2']) ?></b><br />
        <?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount3']) ?>  
        </a>
    </p>
    <p class="f_half left"> 
        <a href="http://www.savingsulove.com/artwork" title="Savings U Love Artwork Approval">
        <img src="<?php echo IMAGE_PATH; ?>artwork1.png" style="float:left; padding-right:20px; margin-top:10px;" />
        <b>Artwork</b><br />
        Approve your artwork  
        </a> 
    </p>
    </div> 


    <div class="full clearfix border_t box"> <br />

    <p class="f_half left"> 
        <a href="<?php echo get_option("manage_url"); ?>" title="<?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount6']) ?>">
        <img src="<?php echo IMAGE_PATH; ?>a3.png" style="float:left; padding-right:20px; margin-top:10px;" />
        <b>Manage My Offers</b><br />
        <?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount7']) ?>
        </a>
    </p>
    <p class="f_half left"> 
        <a href="<?php echo get_option('submit_url'); ?>" title="<?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount8']) ?>">
        <img src="<?php echo IMAGE_PATH; ?>a5.png" style="float:left; padding-right:20px; margin-top:10px;" />
        <b>Create New Offers</b><br />
       <?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount9']) ?>
        </a> 
    </p>
    </div>  

    <div class="full clearfix border_t box"> <br />
    <p class="f_half left"> 
        <a href="<?php echo get_option("messages_url"); ?>" title="<?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount4']) ?>">
        <img src="<?php echo IMAGE_PATH; ?>a2.png" style="float:left; padding-right:20px; margin-top:10px;" />
        <b><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount4']) ?></b><br />
        <?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount5']) ?>  
        </a> 
    </p>  
    <p class="f_half left"> 
        <a href="<?php echo get_option("contact_url"); ?>" title="<?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount10']) ?>">
        <img src="<?php echo IMAGE_PATH; ?>a4.png" style="float:left; padding-right:20px; margin-top:10px;" />
        <b><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount10']) ?></b><br />
        <?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount11']) ?> 
        </a>

    </p>
    </div>

    <div class="full clearfix border_t box"> <br />

    <p class="f_half left"> 
        <a href="<?php  echo wp_logout_url(); ?>" title="<?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount12']) ?>">
        <img src="<?php echo IMAGE_PATH; ?>a6.png" style="float:left; padding-right:20px; margin-top:10px;" />
        <b><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount12']) ?></b><br />
        <?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount13']) ?>
        </a> 
    </p>

    </div>

</div> <!-- end fieldset -->

</div><!-- end middleSidebar -->          

</div>


<div id="MyDetails" style="display:none;">

<h1 class="page-title"> Account Details<?php // echo SPEC($GLOBALS['_LANG']['_tpl_myaccount1']) ?></h1>

<p class="return-link"><a href="<?php echo $GLOBALS['premiumpress']['dashboard_url']; ?>" title="<?php echo SPEC($GLOBALS['_LANG']['_head4']) ?>">Back</a></p> 

<div class="middleSidebar left padding-form" style="width: 710px;">   

<form action="" method="post" enctype="multipart/form-data"> 
<input type="hidden" name="action" value="1" />

<fieldset> 


<div class="full clearfix border_t box"> 
<p class="f_half left"> 
    <label for="name"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount15']) ?> <span class="required"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount16']) ?></span></label><br /> 
    <input type="text" value="<?php echo $userdata->user_login; ?>" disabled class="short" /><br /> 
</p> 
<p class="f_half left"> 
    <label for="email">Email <span class="required">*</span></label><br /> 
    <input type="text" name="form[user_email]" value="<?php echo $userdata->user_email; ?>" class="short" tabindex="10" /><br /> 
</p> 
</div> 


<div class="full clearfix border_t box"> 
<p class="f_half left"> 
    <label for="name"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount17']) ?> <span class="required">*</span></label><br /> 
    <input type="text" name="form[user_url]" value="<?php echo $userdata->user_url; ?>" class="short" tabindex="11" /><br /> 
</p> 
<p class="f_half left"> 
    <label for="comment2"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount18']) ?> <span class="required">*</span></label><br /> 
    <textarea tabindex="5" class="short" rows="4" name="form[description]"><?php echo $userdata->description; ?></textarea><br /> 

</p> 
</div>

</fieldset>                     

  <h3 class="inpage-header"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount19']) ?></h3>

<fieldset> 

<div class="full clearfix border_t box"> 
<p class="f_half left"> 
    <label for="name"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount20']) ?> <span class="required">*</span></label><br /> 
    <input type="text" name="form[first_name]" value="<?php echo $userdata->first_name; ?>" class="short" tabindex="12" /><br /> 

</p> 
<p class="f_half left"> 
    <label for="email"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount21']) ?> <span class="required">*</span></label><br /> 
    <input type="text" name="form[last_name]" value="<?php echo $userdata->last_name; ?>" class="short" tabindex="13" /><br /> 

</p> 
</div>  


 <div class="full clearfix border_t box"> 

<p class="f_half left"> 
    <label for="email"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount25']) ?> <span class="required">*</span></label><br /> 
    <input type="text" name="address[address]" value="<?php echo $ADD[2]; ?>" class="short" tabindex="17" /><br /> 

</p>

<p class="f_half left"> 
    <label for="address[state]"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount23']) ?> <span class="required">*</span></label><br /> 
    <select type="text" name="address[state]" id="first" value="<?php echo $ADD[1]; ?>" class="short" tabindex="15"><option value=""></option></select><br />

</p>

</div>  


 <div class="full clearfix border_t box">

 <p class="f_half left"> 
    <label for="address[city]"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount24']) ?> <span class="required">*</span></label><br /> 
    <select type="text" name="address[city]" id="second" value="<?php echo $ADD[3]; ?>" class="short" tabindex="16"><option value=""></option></select><br /> 

</p>

<p class="f_half left"> 
    <label for="name"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount26']) ?> <span class="required">*</span></label><br /> 
    <input type="text" name="address[zip]" value="<?php echo $ADD[4]; ?>" class="short" tabindex="18" /><br /> 

</p> 

</div>


 <div class="full clearfix border_t box"> 
<p class="f_half left"> 
    <label for="email"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount27']) ?> <span class="required">*</span></label><br /> 
    <input type="text" name="address[phone]" value="<?php echo $ADD[5]; ?>" class="short" tabindex="19" /><br /> 

</p> 
</div>

<?php if ($current_user->ID == 1){ ?>


<div class="full clearfix border_t box"> 
  <p class="f_half left"> 
      <label for="address2">Address 2</label><br />
      <input type="text" name="address2" id="address2" value="<?php echo esc_attr( get_the_author_meta( 'address2', $userId ) ); ?>" class="short" /><br />
      <span class="description">E.g. Street Address, City, State Zipcode</span>
  </p>
</div> 

<div class="full clearfix border_t box"> 
  <p class="f_half left"> 
      <label for="address3">Address 3</label><br />
      <input type="text" name="address3" id="address3" value="<?php echo esc_attr( get_the_author_meta( 'address3', $userId ) ); ?>" class="short" /><br />
      <span class="description">E.g. Street Address, City, State Zipcode</span> 
  </p> 
</div>

<div class="full clearfix border_t box"> 
  <p class="f_half left"> 
      <label for="hours">Hours</label><br />
      <input type="text" name="hours" id="hours" value="<?php echo esc_attr( get_the_author_meta( 'hours', $userId ) ); ?>" class="short" /><br />
      <span class="description">Enter your hours of operation. (Optional)</span>
  </p> 
</div>

<?php } ?> 

</fieldset>

 <h3 class="inpage-header"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount30']) ?></h3>

<fieldset> 

<div class="full clearfix border_t box"> 

<p class="f_half left"> 
    <label for="name"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount31']) ?> <span class="required">*</span></label><br /> 
    <input type="text" name="password" class="short" tabindex="20" /><br /> 

</p> 
<p class="f_half left"> 
    <label for="email"><?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount32']) ?> <span class="required">*</span></label><br /> 
    <input type="text" name="password_r" class="short" tabindex="21" /><br /> 

</p> 
</div>                     


</fieldset>

<?php if ($current_user->ID == 1){ ?>

<?php 

add_action( 'personal_options_update', 'my_save_extra_user_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_user_fields' );

function my_save_extra_user_fields( $userId ) {

    if ( !current_user_can( 'edit_user', $userId ) )
        return false;

    update_usermeta( $userId, 'address2', $_POST['address2'] );
    update_usermeta( $userId, 'address3', $_POST['address3'] );
    update_usermeta( $userId, 'hours', $_POST['hours'] );
}

?>

<?php } ?>                        

<div class="full clearfix border_t box"><p class="full clearfix"> 
<input type="submit" name="submit" id="submit" class="button grey" tabindex="15" value="<?php echo SPEC($GLOBALS['_LANG']['_tpl_myaccount33']) ?>" /> 
</p></div>

</form> 

</div>

</div> </div>

<?php get_footer(); ?>

You can that I created three test meta fields (address 1, address 2, and hours), but Im not able to save them from the front end of the site.

share|improve this question
    
Could I have a look at the whole template code for the front end edition? –  Joshua Abenazer Nov 23 '11 at 12:07
    
Hi Joshua, sure. I updated my post with the code from that template. –  user1893 Nov 30 '11 at 16:18
add comment

2 Answers 2

up vote 1 down vote accepted

"edit_user_profile_update" is not an action... check: http://codex.wordpress.org/Plugin_API/Action_Reference

update_usermeta() is deprecated in wp 3 and above, use update_user_meta()

your form is lacking some standard hidden fields like:

<input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr( $current_user->ID ); ?>" />

use wp_nonce_field(); ex: <?php wp_nonce_field( 'update-user_' . $current_user->ID ) ?>

share|improve this answer
2  
Actually, 'edit_user_profile_update' is an action... see the source of wp-admin/user-edit.php. However, its only called when editing someone else's profile. The action called when editing your own profile page - which I assume this code is intended for - is called personal_options_update. –  goldenapples Feb 21 '12 at 23:47
add comment

But when I update the page it shows the previous values saved in the back-end (admin) section of wordpress. Of course, any values saved in the back end are saved correctly and displayed on the front-end. I wanted to save either way, from back-end and from front-end.

Just had this problem myself.

Given that the meta was in fact updating but just not showing I just decided to go for

    <?php  if (isset($_POST['address_field'])) { 
        echo $_POST['address_field']; }
        else { echo get_user_meta( get_current_user_id(), 'address_field', true );

?>

That showed the values it was updating to

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.