在帐户页面的前端显示用户角色

时间:2019-10-21 15:45:34

标签: wordpress

我正在尝试允许客户在其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' ),
        ),
    ),
) );

}

1 个答案:

答案 0 :(得分:0)

使用以下功能获取用户的当前角色

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

对您共享的功能进行如下修改,我添加了'value'参数。

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'); 

让我知道哪种方法最适合您。