admin管理员组文章数量:1130349
I've added a function to my child-theme to add a new field in the Add New User section so that you can select if the member is either just a member or candidate etc :
function show_extra_profile_fields( $user ) {
$previous_value = '';
if( is_object($user) && isset($user->ID) ) {
$previous_value = get_user_meta( $user->ID, 'membership', true );
}
?>
<hr>
<h2>Membership Status</h2>
<table class="form-table">
<tr>
<th><label for="membership">Membership</label></th>
<td>
<select name="membership" id="membership" style="width:150px;">
<option value="Member" <?php selected( 'Member', get_the_author_meta( 'membership', $user->ID ) ); ?>>Member</option>
<option value="Candidate" <?php selected( 'Candidate', get_the_author_meta( 'membership', $user->ID ) ); ?>>Candidate</option>
</select>
</td>
</tr>
</table>
<hr>
<?php }
add_action( 'show_user_profile', 'show_extra_profile_fields' );
add_action( 'edit_user_profile', 'show_extra_profile_fields' );
add_action( "user_new_form", "show_extra_profile_fields" );
And my Save Function:
function save_extra_profile_fields( $user_id ) {
# save choice
if( isset($_POST['membership']) ) {
update_user_meta( $user_id, 'membership', $_POST['membership'] );
}
}
add_action( "user_new_form", "save_extra_profile_fields" );
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
My problem is that once I click create it doesn't save my option I've selected in the select box. It does save once I go into the user and then change it again.
What might I have missed that causes the choice not to save on creation?
I've added a function to my child-theme to add a new field in the Add New User section so that you can select if the member is either just a member or candidate etc :
function show_extra_profile_fields( $user ) {
$previous_value = '';
if( is_object($user) && isset($user->ID) ) {
$previous_value = get_user_meta( $user->ID, 'membership', true );
}
?>
<hr>
<h2>Membership Status</h2>
<table class="form-table">
<tr>
<th><label for="membership">Membership</label></th>
<td>
<select name="membership" id="membership" style="width:150px;">
<option value="Member" <?php selected( 'Member', get_the_author_meta( 'membership', $user->ID ) ); ?>>Member</option>
<option value="Candidate" <?php selected( 'Candidate', get_the_author_meta( 'membership', $user->ID ) ); ?>>Candidate</option>
</select>
</td>
</tr>
</table>
<hr>
<?php }
add_action( 'show_user_profile', 'show_extra_profile_fields' );
add_action( 'edit_user_profile', 'show_extra_profile_fields' );
add_action( "user_new_form", "show_extra_profile_fields" );
And my Save Function:
function save_extra_profile_fields( $user_id ) {
# save choice
if( isset($_POST['membership']) ) {
update_user_meta( $user_id, 'membership', $_POST['membership'] );
}
}
add_action( "user_new_form", "save_extra_profile_fields" );
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
My problem is that once I click create it doesn't save my option I've selected in the select box. It does save once I go into the user and then change it again.
What might I have missed that causes the choice not to save on creation?
Share Improve this question asked Oct 23, 2018 at 13:49 DemonixDemonix 613 silver badges12 bronze badges 01 Answer
Reset to default 0Try hooking your save function to user_register, because it could be that the $user_id you have in your function doesn't exists yet so there's no user to attach the meta to.
I've added a function to my child-theme to add a new field in the Add New User section so that you can select if the member is either just a member or candidate etc :
function show_extra_profile_fields( $user ) {
$previous_value = '';
if( is_object($user) && isset($user->ID) ) {
$previous_value = get_user_meta( $user->ID, 'membership', true );
}
?>
<hr>
<h2>Membership Status</h2>
<table class="form-table">
<tr>
<th><label for="membership">Membership</label></th>
<td>
<select name="membership" id="membership" style="width:150px;">
<option value="Member" <?php selected( 'Member', get_the_author_meta( 'membership', $user->ID ) ); ?>>Member</option>
<option value="Candidate" <?php selected( 'Candidate', get_the_author_meta( 'membership', $user->ID ) ); ?>>Candidate</option>
</select>
</td>
</tr>
</table>
<hr>
<?php }
add_action( 'show_user_profile', 'show_extra_profile_fields' );
add_action( 'edit_user_profile', 'show_extra_profile_fields' );
add_action( "user_new_form", "show_extra_profile_fields" );
And my Save Function:
function save_extra_profile_fields( $user_id ) {
# save choice
if( isset($_POST['membership']) ) {
update_user_meta( $user_id, 'membership', $_POST['membership'] );
}
}
add_action( "user_new_form", "save_extra_profile_fields" );
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
My problem is that once I click create it doesn't save my option I've selected in the select box. It does save once I go into the user and then change it again.
What might I have missed that causes the choice not to save on creation?
I've added a function to my child-theme to add a new field in the Add New User section so that you can select if the member is either just a member or candidate etc :
function show_extra_profile_fields( $user ) {
$previous_value = '';
if( is_object($user) && isset($user->ID) ) {
$previous_value = get_user_meta( $user->ID, 'membership', true );
}
?>
<hr>
<h2>Membership Status</h2>
<table class="form-table">
<tr>
<th><label for="membership">Membership</label></th>
<td>
<select name="membership" id="membership" style="width:150px;">
<option value="Member" <?php selected( 'Member', get_the_author_meta( 'membership', $user->ID ) ); ?>>Member</option>
<option value="Candidate" <?php selected( 'Candidate', get_the_author_meta( 'membership', $user->ID ) ); ?>>Candidate</option>
</select>
</td>
</tr>
</table>
<hr>
<?php }
add_action( 'show_user_profile', 'show_extra_profile_fields' );
add_action( 'edit_user_profile', 'show_extra_profile_fields' );
add_action( "user_new_form", "show_extra_profile_fields" );
And my Save Function:
function save_extra_profile_fields( $user_id ) {
# save choice
if( isset($_POST['membership']) ) {
update_user_meta( $user_id, 'membership', $_POST['membership'] );
}
}
add_action( "user_new_form", "save_extra_profile_fields" );
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
My problem is that once I click create it doesn't save my option I've selected in the select box. It does save once I go into the user and then change it again.
What might I have missed that causes the choice not to save on creation?
Share Improve this question asked Oct 23, 2018 at 13:49 DemonixDemonix 613 silver badges12 bronze badges 01 Answer
Reset to default 0Try hooking your save function to user_register, because it could be that the $user_id you have in your function doesn't exists yet so there's no user to attach the meta to.
本文标签: functionsCreate New User Custom Field not Saving
版权声明:本文标题:functions - Create New User Custom Field not Saving 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749238115a2337622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论