首页 > 解决方案 > 在帐户页面的前端显示用户角色

问题描述

我试图让客户在他们的 WordPress 帐户页面上更改他们的角色,并且除了前端显示的内容之外,所有功能都运行良好。当他们来改变他们的角色时,选择的选项总是“选择一个选项......”而不是他们被指定的实际角色(例如示例 1)。我已经排除了一些角色(例如管理员),因为我只想向他们展示某些角色。我在下面的代码中做了什么改变来解决这个问题并显示他们当前的角色?

            function iconic_get_account_fields() {

            return apply_filters( 'iconic_account_fields', array(

            'first_name'                 => array(

            'type'                 => 'text',

            'label'                => __( 'First Name', 'iconic' ),

            'hide_in_account'      => true,

            'hide_in_admin'        => true,

            'hide_in_checkout'     => true,

            'hide_in_registration' => false,

            'required'             => true,

            ),

            'last_name'                  => array(

            'type'                 => 'text',

            'label'                => __( 'Last Name', 'iconic' ),

            'hide_in_account'      => true,

            'hide_in_admin'        => true,

            'hide_in_checkout'     => true,

            'hide_in_registration' => false,

            'required'             => true,

            ),

            'role'                  => array(

            'type'    => 'select',

            'label'   => __( 'Franking Machine Model', 'iconic' ),

            'hide_in_account'      => false,

            'hide_in_admin'        => true,

            'hide_in_registration' => true,

            'hide_in_checkout'     => true,

            'options' => array(

            '' => __( 'Select an option...', 'iconic' ),

            'example_1_customer'  => __( 'Example 1', 'iconic' ),

            'example_2_customer'  => __( 'Example 2', 'iconic' ),

            'example_3_customer'  => __( 'Example 3', 'iconic' ),

            'other_customer'  => __( 'Other', 'iconic' ),
        ),
    ),
) );

}

标签: wordpress

解决方案


使用以下函数获取用户的当前角色

function get_current_user_role(){
    $current_user = wp_get_current_user();
    $roles = $current_user->roles;
    return $roles[0];
}

解决方案#1

将其传递给woocommerce_form_field($key, $args, $value)函数,考虑以下示例,

    $fields = iconic_get_account_fields();
    $role = get_current_user_role();
    foreach ($fields as $key => $field_args) {
        if ($key == 'role') {
            woocommerce_form_field($key, $field_args, $role);
        } else {
            woocommerce_form_field($key, $field_args);
        }
    }

解决方案#2

修改您共享的功能,如下所示,我添加了“值”参数。

function iconic_get_account_fields() {
    return apply_filters('iconic_account_fields', array(
        'first_name' => array(
            'type' => 'text',
            'label' => __('First Name', 'iconic'),
            'hide_in_account' => true,
            'hide_in_admin' => true,
            'hide_in_checkout' => true,
            'hide_in_registration' => false,
            'required' => true,
        ),
        'last_name' => array(
            'type' => 'text',
            'label' => __('Last Name', 'iconic'),
            'hide_in_account' => true,
            'hide_in_admin' => true,
            'hide_in_checkout' => true,
            'hide_in_registration' => false,
            'required' => true,
        ),
        'role' => array(
            'type' => 'select',
            'label' => __('Franking Machine Model', 'iconic'),
            'hide_in_account' => false,
            'hide_in_admin' => true,
            'hide_in_registration' => true,
            'hide_in_checkout' => true,
            'options' => array(
                '' => __('Select an option...', 'iconic'),
                'example_1_customer' => __('Example 1', 'iconic'),
                'example_2_customer' => __('Example 2', 'iconic'),
                'example_3_customer' => __('Example 3', 'iconic'),
                'other_customer' => __('Other', 'iconic'),
            ),
            'value' => apply_filters('selectedrole','')  // added new arg
        ),
 ));

add_filter('selectedrole', 'get_current_user_role'); 

让我知道哪个最适合你。


推荐阅读