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' ),
),
),
) );
}
解决方案
使用以下函数获取用户的当前角色
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');
让我知道哪个最适合你。
推荐阅读
- go - 查询的命令行参数
- javascript - 带索引位置的 For..in 循环数组迭代
- python - 基于多个条件(lt、gt 测试标准)将数据帧索引作为值分配给另一个数据帧
- android - Android中的getSize()是否包含状态栏的高度?
- java - 为什么我不能从我的主要使用静态方法?
- sql - 回车未从 Access 中导出的 .csv 中删除
- sccm - SCCM包中多个返回码的返回和报告
- amazon-web-services - 在 AWS CodePipeline 中集成 SonarQube:拒绝连接
- arrays - 从对象中获取值作为Powershell中的数组
- java - 取自文本文件的字符数组:索引超出范围