最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Create New User Custom Field not Saving

matteradmin9PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far