面向用户的前端用户元选项

时间:2011-11-16 作者:user1893

我使用本教程创建了用户元选项。现在我想知道如何允许用户编辑(更新)我创建的某些自定义用户元选项。

用户已经有了一个模板帐户页面,可以在其中访问一些用户元选项。我在用户页面中添加了a函数,以在单击页面上的更新按钮时更新元字段。

Im为新用户元使用文本输入字段。这是我用来更新选项的代码:

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
}
但当我更新页面时,它会显示wordpress后端(管理)部分中保存的先前值。当然,任何保存在后端的值都会正确保存并显示在前端。我想从后端和前端两种方式进行保存。

我做错了什么?如何修复此问题?

谢谢

UPDATE:

以下是用户用于更新其现有元信息的模板页面的代码:

<?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(); ?>
你可以知道,我创建了三个测试元字段(地址1、地址2和小时),但我无法从网站前端保存它们。

2 个回复
最合适的回答,由SO网友:Tribalpixel 整理而成

“edit\\u user\\u profile\\u update”不是一个操作。。。检查:http://codex.wordpress.org/Plugin_API/Action_Reference

update\\u usermeta()在wp 3及以上版本中不推荐使用,请使用update\\u user\\u meta()

您的表单缺少一些标准的隐藏字段,如:

<input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr( $current_user->ID ); ?>" />
使用wp\\u nonce\\u field();例如:<?php wp_nonce_field( \'update-user_\' . $current_user->ID ) ?>

SO网友:Bysander

但当我更新页面时,它会显示wordpress后端(管理)部分中保存的先前值。当然,任何保存在后端的值都会正确保存并显示在前端。我想从后端和前端两种方式进行保存。

我自己也有这个问题。

考虑到meta实际上正在更新,但没有显示,我决定

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

显示了更新到的值

结束