如何在wordpress multisite中为新用户注册添加新字段到注册页面?

时间:2016-02-23 12:08:25

标签: php wordpress

我想在用户注册页面中添加自定义字段。如何设置在当前表单中添加字段?我尝试了下面的代码,但它不起作用,register_fromregister_postuser_register挂钩也没有用。

add_action('register_form','show_first_name_field');
add_action('register_post','check_fields',10,3);
add_action('user_register', 'register_extra_fields');

function show_first_name_field()
{
?>
    <p>
    <label>Twitter<br/>
    <input id="twitter" type="text" tabindex="30" size="25" value="<?php echo $_POST['twitter']; ?>" name="twitter" />
    </label>
    </p>
<?php
}

function check_fields ( $login, $email, $errors )
{
    global $twitter;
    if ( $_POST['twitter'] == '' )
    {
        $errors->add( 'empty_realname', "<strong>ERROR</strong>: Please Enter your twitter handle" );
    }
    else
    {
        $twitter = $_POST['twitter'];
    }
}

function register_extra_fields ( $user_id, $password = "", $meta = array() )
{
    update_user_meta( $user_id, 'twitter', $_POST['twitter'] );
}

2 个答案:

答案 0 :(得分:0)

以下是官方文档中的链接,请尝试以下操作:Add a field to register

答案 1 :(得分:0)

我解决了它。我使用下面的代码在注册页面中添加自定义字段

/**

 * Add additional custom field to profile page

 */



add_action ( 'show_user_profile', 'my_show_extra_profile_fields' );

add_action ( 'edit_user_profile', 'my_show_extra_profile_fields' );



function my_show_extra_profile_fields ( $user )

{

?>

    <h3>Extra profile information</h3>

    <table class="form-table">

        <tr>

            <th><label for="twitter">Twitter</label></th>

            <td>

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

                <span class="description">Please enter your Twitter username.</span>

            </td>

        </tr>

    </table>

<?php

}



/**

 * Save data input from custom field on profile page

 */



add_action ( 'personal_options_update', 'my_save_extra_profile_fields' );

add_action ( 'edit_user_profile_update', 'my_save_extra_profile_fields' );



function my_save_extra_profile_fields( $user_id )

{

    if ( !current_user_can( 'edit_user', $user_id ) )

        return false;

    /* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */

    update_usermeta( $user_id, 'twitter', $_POST['twitter'] );

}





/**

 * Add cutom field to WPMU registration form

 */

add_action('signup_extra_fields','custom_signup_extra_fields', $errors);



function custom_signup_extra_fields($errors) {

?>

    <label>Twitter</label>

    <input id="twitter" type="text" size="25" value="<?php echo $_POST['twitter']; ?>" name="twitter" />

    <br />



<?php

}



/**

 * Save custom field input to wp_signups table

 */

add_filter( 'add_signup_meta', 'custom_add_signup_meta' );



function custom_add_signup_meta ( $meta = array() ) {



    $meta['twitter'] = $_POST['twitter'];



    return $meta;



}