最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why is my Custom User Meta deleted on Profile Update?

programmeradmin5浏览0评论

I have problems with my custom user fields on updating the user profile withing the /wp-admin/user-edit.php. The values of the huna and ki field are deleted. The validation and altUserID still show up correctly. Where is my error in the code? Thanks for your help!

Before Updating the User

After Updating the User

My Code

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
    <h3><?php _e("Magic Link", "blank"); ?></h3>

    <table class="form-table">
    <tr>
        <th><label for="Secret">Secret</label></th>
        <td>
            <input type="text" name="secret" id="secret" value="<?php echo esc_attr( get_the_author_meta( 'huna', $user->ID ) ); ?>" class="regular-text" /><br />
        </td>
    </tr>
    <tr>
        <th><label for="Key">Key</label></th>
        <td>
            <input type="text" name="key" id="key" value="<?php echo esc_attr( get_the_author_meta( 'ki', $user->ID ) ); ?>" class="regular-text" /><br />
        </td>
    </tr>
    <tr>
        <th><label for="Validation">Validation</label></th>
        <td>
            <input type="text" name="validation" id="validation" value="<?php echo esc_attr( get_the_author_meta( 'validation', $user->ID ) ); ?>" class="regular-text" /><br />
        </td>
    </tr>
    <tr>
        <th><label for="Validation">Welcome Screen</label></th>
        <td>
            <input type="checkbox" name="welcome" id="welcome" value="<?php echo esc_attr( get_the_author_meta( 'welcome', $user->ID ) ); ?>" class="regular-text" /><br />

        </td>
    </tr>
    </table>

    <h3>Misc</h3>

    <table class="form-table">
    <tr>
        <th><label for="altID">Alternate ID</label></th>
        <td>
            <input type="text" name="altUserID" id="altUserID" value="<?php echo esc_attr( get_the_author_meta( 'altUserID', $user->ID ) ); ?>" class="regular-text" /><br />
        </td>
    </tr>
    
    </table>
<?php }

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, 'huna', $_POST['huna'] );
    update_user_meta( $user_id, 'ki', $_POST['ki'] );
    update_user_meta( $user_id, 'validation', $_POST['validation'] );
    update_user_meta( $user_id, 'welcome', $_POST['welcome'] );
    update_user_meta( $user_id, 'altUserID', $_POST['altUserID'] );
}
发布评论

评论列表(0)

  1. 暂无评论