functions - Create New User Custom Field not Saving

admin2025-06-07  48

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 0
Add a comment  | 

1 Answer 1

Reset to default 0

Try 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749238138a317518.html

最新回复(0)