是否可以将特定自定义程序选项的选择限制在用户角色上?
现在,我有一个自定义控件:
$wp_customize->add_control( 'checkin[theme]', array(
'label' => __('Theme', 'greet'),
// 'description' => 'Set theme',
'section' => 'checkin',
'type' => 'select',
'choices' => array(
'theme-1' => 'Background',
'theme-2' => 'Stars (Live)',
'theme-3' => 'Gradient (Live)',
),
) );我想将theme-2和theme-3限制为只对特定的用户角色可用,或者是不可用的,或者是其他角色的灰色/禁用。
如何使用数组完成此操作?
发布于 2020-11-04 09:40:48
有两种方法可以做到这一点。
capability:$wp_customize->add_setting(
'display_excerpt_or_full_post',
array(
'capability' => 'edit_theme_options',
'default' => 'excerpt',
'sanitize_callback' => function( $value ) {
return 'excerpt' === $value || 'full' === $value ? $value : 'excerpt';
},
)
);render_callback参数来确定是否显示该控件:$wp_customize->add_control( 'checkin[theme]', array(
'label' => __('Theme', 'greet'),
'section' => 'checkin',
'type' => 'select',
'choices' => array(
'theme-1' => 'Background',
'theme-2' => 'Stars (Live)',
'theme-3' => 'Gradient (Live)',
),
),
'render_callback' => function() {
return current_user_can( 'edit_posts' );
},
);请注意,这些是完全不同的。在设置中使用capability参数将完全跳过控件,并且它不会添加到定制器API中。使用render_callback参数,控件仍将添加到自定义程序中,并且它将从API中获得,它将被隐藏。如果隐藏(但仍然暴露在API中),我可以打开控制台并这样做来显示它--尽管您已经隐藏了它:
wp.customize.control( 'checkin[theme]' ).activate()https://wordpress.stackexchange.com/questions/377594
复制相似问题