Wordpress 如何使用 admin_head 挂钩为特定角色应用 css
Wordpress how to apply css for specific roles using admin_head hook
我想添加一行代码以将 css
应用于某些角色。
例如:
如果角色“编辑”:
#wp-admin-bar-top-secondary{
display:none;
}
我的代码段:
add_action('admin_head', 'my_custom_style');
function my_custom_style(){
echo '<style>
/*remove media button*/
.wp-media-buttons {
display: none;
}
/*remove visual&code tabs*/
.wp-editor-tabs {
display: none;
}
</style>';
}
您可以使用从 wp_get_current_user
Docs 函数返回的用户对象的 roles
属性。
add_action('admin_head', 'my_custom_style');
function my_custom_style()
{
$roles = wp_get_current_user()->roles;
if (!in_array('administrator', $roles))
{
?>
<style>
/*remove media button*/
.wp-media-buttons {
display: none;
}
/*remove visual&code tabs*/
.wp-editor-tabs {
display: none;
}
</style>
<?php
};
}
注:
- 有些用户可能有不止一个角色,这就是我使用
in_array
功能的原因!
- 当然你可以用感叹号/'not operator'做相反的事情。 (即
!in_array()
)
我想添加一行代码以将 css
应用于某些角色。
例如:
如果角色“编辑”:
#wp-admin-bar-top-secondary{
display:none;
}
我的代码段:
add_action('admin_head', 'my_custom_style');
function my_custom_style(){
echo '<style>
/*remove media button*/
.wp-media-buttons {
display: none;
}
/*remove visual&code tabs*/
.wp-editor-tabs {
display: none;
}
</style>';
}
您可以使用从 wp_get_current_user
Docs 函数返回的用户对象的 roles
属性。
add_action('admin_head', 'my_custom_style');
function my_custom_style()
{
$roles = wp_get_current_user()->roles;
if (!in_array('administrator', $roles))
{
?>
<style>
/*remove media button*/
.wp-media-buttons {
display: none;
}
/*remove visual&code tabs*/
.wp-editor-tabs {
display: none;
}
</style>
<?php
};
}
注:
- 有些用户可能有不止一个角色,这就是我使用
in_array
功能的原因! - 当然你可以用感叹号/'not operator'做相反的事情。 (即
!in_array()
)