【问题标题】:add short description to fields为字段添加简短描述
【发布时间】:2018-12-06 17:50:15
【问题描述】:

我有这个功能可以在 WordPress 管理员用户个人资料页面中添加新的用户字段。

function modify_user_contact_methods( $user_contact ) {

    // Add user contact methods
    $user_contact['skype']   = __( 'Skype Username'   );
    $user_contact['phone'] = __( 'Phone' );

    return $user_contact;
}
add_filter( 'user_contactmethods', 'modify_user_contact_methods' );

上面的代码只显示了字段,我想在它后面加上一个描述,如下所示。

<input type="text" name="phone" id="phone" value="" class="regular-text">
<p class="description">Your phone number.</p>

如何添加类似上面的简短描述?

【问题讨论】:

  • 使用上面的钩子是不可能的。

标签: php wordpress function input


【解决方案1】:

如果你正在寻找描述,你不能使用上面的钩子。但是,您可以使用以下方法。

输出字段的代码

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

function extra_user_profile_fields( $user ) { ?>
    <h2><?php _e("Extra Contact Info", "textdomain"); ?></h2>

    <table class="form-table">
    <tr>
        <th><label for="skype"><?php _e("Skype Username"); ?></label></th>
        <td>
            <input type="text" name="skype" id="skype" value="<?php echo esc_attr( get_the_author_meta( 'skype', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("Please enter your skype."); ?></span>
        </td>
    </tr>
    <tr>
        <th><label for="phone"><?php _e("Phone"); ?></label></th>
        <td>
            <input type="text" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("Please enter your phone."); ?></span>
        </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, 'skype', $_POST['skype'] );
    update_user_meta( $user_id, 'phone', $_POST['phone'] );
}

【讨论】:

    猜你喜欢
    • 2017-09-17
    • 2020-08-28
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2012-07-21
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多