【问题标题】:Show User Role On Front End Of Account Page在帐户页面的前端显示用户角色
【发布时间】:2019-10-22 18:35:00
【问题描述】:

我试图让客户在他们的 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


    【解决方案1】:

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

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

    让我知道哪个最适合你。

    【讨论】:

    • 嗨,非常感谢,第二个解决方案很有效,完全符合我的要求。添加了函数 get_current_user_role 然后过滤器帮助解决了这个问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2016-03-24
    • 2017-02-17
    • 1970-01-01
    • 2018-05-19
    相关资源
    最近更新 更多