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

Extra profile field as select box?

programmeradmin1浏览0评论

I want to add some more profile fields to Wordpress. It works fine with normal text box:

<input type="text" name="gender" id="gender" value="<?php echo esc_attr( get_the_author_meta( 'gender', $user->ID ) ); ?>" class="regular-text" /><br />

But of course, I don't want my users to enter "male" or "female". So I need a select box! :)

May you please help me with how I have to change the code above to get a select box?!

Thanks a lot!

I want to add some more profile fields to Wordpress. It works fine with normal text box:

<input type="text" name="gender" id="gender" value="<?php echo esc_attr( get_the_author_meta( 'gender', $user->ID ) ); ?>" class="regular-text" /><br />

But of course, I don't want my users to enter "male" or "female". So I need a select box! :)

May you please help me with how I have to change the code above to get a select box?!

Thanks a lot!

Share Improve this question edited Dec 1, 2020 at 2:15 RyanS 1256 bronze badges asked Nov 20, 2011 at 19:11 PhilippPhilipp 1192 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 10

Place the following code in your functions.php

<?php
    add_action( 'show_user_profile', 'show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'show_extra_profile_fields' );

    function show_extra_profile_fields( $user ) { ?>
        <h3>Extra profile information</h3>
        <table class="form-table">
            <tr>
                <th><label for="gender">Gender</label></th>
                <td>
                    <select name="gender" id="gender" >
                        <option value="Male" <?php selected( 'Male', get_the_author_meta( 'gender', $user->ID ) ); ?>>Male</option>
                        <option value="Female" <?php selected( 'Female', get_the_author_meta( 'gender', $user->ID ) ); ?>>Female</option>
                    </select>
                </td>
            </tr>
        </table>
    <?php }

    add_action( 'personal_options_update', 'save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );

    function save_extra_profile_fields( $user_id ) {
        if ( !current_user_can( 'edit_user', $user_id ) )
            return false;

        //typo fix
        update_user_meta( $user_id, 'gender', $_POST['gender'] );
    }
?>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论