Php 如何在wp中更新自定义用户元字段?
我使用以下命令在用户配置文件编辑器屏幕上添加和显示新的用户元字段,但当我单击更新按钮时,值不会更改:Php 如何在wp中更新自定义用户元字段?,php,wordpress,Php,Wordpress,我使用以下命令在用户配置文件编辑器屏幕上添加和显示新的用户元字段,但当我单击更新按钮时,值不会更改: function extra_user_profile_fields( $user ) { ?> <table class="form-table"> <tr> <th><label for="save_post_external"><?php _e("Save posts external"
function extra_user_profile_fields( $user ) { ?>
<table class="form-table">
<tr>
<th><label for="save_post_external"><?php _e("Save posts external"); ?></label></th>
<td>
<input type="text" name="save_post_external" id="save_post_external" value="<?php echo esc_attr( get_the_author_meta( 'save_post_external', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
<tr>
<th><label for="save_post_internal"><?php _e("Save posts internal"); ?></label></th>
<td>
<input type="text" name="save_post_internal" id="save_post_internal" value="<?php echo esc_attr( get_the_author_meta( 'save_post_internal', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'Save Posts', $_POST['save_post_external'] );
update_user_meta( $user_id, 'Save Posts', $_POST['save_post_internal'] );
}
function-extra\u-user\u-profile\u字段($user){?>
一切都很好,只需要修复两行。您刚刚错过了正确的用户名
update_user_meta( $user_id, 'save_post_external', $_POST['save_post_external'] );
update_user_meta( $user_id, 'save_post_internal', $_POST['save_post_internal'] );
此外,以下是工作代码:
function extra_user_profile_fields( $user ) { ?>
<table class="form-table">
<tr>
<th><label for="save_post_external"><?php _e("Save posts external"); ?></label></th>
<td>
<input type="text" name="save_post_external" id="save_post_external" value="<?php echo esc_attr( get_the_author_meta( 'save_post_external', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
<tr>
<th><label for="save_post_internal"><?php _e("Save posts internal"); ?></label></th>
<td>
<input type="text" name="save_post_internal" id="save_post_internal" value="<?php echo esc_attr( get_the_author_meta( 'save_post_internal', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'save_post_external', $_POST['save_post_external'] );
update_user_meta( $user_id, 'save_post_internal', $_POST['save_post_internal'] );
}
function-extra\u-user\u-profile\u字段($user){?>
我还有最后一个问题,我们可以开始聊天吗?