我对计算机/代码等不是很好。
我使用了一个插件,可以使注册表单变得很重要,然后在表单中添加了国家,年龄段,性别等。我单击将注册器添加到wordpress用户thingy中的选项。但是当我尝试时,后端的“用户”部分仅显示用户名和电子邮件。
其他字段是否可以在用户部分显示?显示它们以供统计用途。

评论

请尝试我们的搜索。您会发现许多示例。

#1 楼

您需要使用show_user_profileedit_user_profilepersonal_options_updateedit_user_profile_update钩子。

您可以使用以下代码在“用户”部分添加其他字段。编辑用户部分:

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("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
    <tr>
        <th><label for="address"><?php _e("Address"); ?></label></th>
        <td>
            <input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("Please enter your address."); ?></span>
        </td>
    </tr>
    <tr>
        <th><label for="city"><?php _e("City"); ?></label></th>
        <td>
            <input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("Please enter your city."); ?></span>
        </td>
    </tr>
    <tr>
    <th><label for="postalcode"><?php _e("Postal Code"); ?></label></th>
        <td>
            <input type="text" name="postalcode" id="postalcode" value="<?php echo esc_attr( get_the_author_meta( 'postalcode', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("Please enter your postal code."); ?></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, 'address', $_POST['address'] );
    update_user_meta( $user_id, 'city', $_POST['city'] );
    update_user_meta( $user_id, 'postalcode', $_POST['postalcode'] );
}


也有关于此主题的几篇博客文章可能会有所帮助:


添加和使用自定义用户配置文件字段
向WordPress用户配置文件添加其他字段


评论


太棒了,这很棒。

– AVEbrahimi
18年3月15日在13:31

这不是在数据库中存储我额外字段中的数据。有什么建议吗?谢谢。

– b_dubb
19年7月3日在16:12

@b_dubb,能否请您分享您的代码?因此,我将检查并告知您。

– Arpita Hunka
19年8月2日在14:38

我已经解决了我的问题,但感谢您的支持。

– b_dubb
19年8月5日在17:31

您应该为此添加随机数验证,以避免引入安全漏洞。 developer.wordpress.org/themes/theme-security/using-nonces

– squarecandy
20年1月30日在18:26

#2 楼

Advanced Custom Fields Pro插件将允许您无需任何编码即可将字段添加到用户配置文件。

评论


仅专业版

–我是最愚蠢的人
19 Mar 4 '19 at 12:16

有免费的方法可以用PHP做到这一点。

– Drmzindec
19-10-15在9:05

是的-如果您愿意,绝对可以在没有ACF的情况下使用PHP编写此代码。我的经验是,它需要100多行代码,并且您需要担心随机数验证,编写表单HTML等问题。与ACF中的5-10分钟的设置相比,可能需要花费几个小时的编码时间。可能取决于您是否已经在项目中使用ACF Pro。

– squarecandy
19-10-15在16:02

Wordpress应该做到这一点,而不要求您在php中对html表单进行硬编码。我是第二个ACF,它应该是核心部分。您还可以使用代码定义字段并对其进行版本控制。

– marek.m
20 Jan 30'14:13

#3 楼

您最好使用get_user_meta(而不是get_the_author_meta):

function extra_user_profile_fields( $user ) {
    $meta = get_user_meta($user->ID, 'meta_key_name', false);
}


评论


两者都没有问题!

–费尔南多·巴尔塔扎(Fernando Baltazar)
20年8月18日在19:04