admin管理员组文章数量:1289543
I have created a custom role (b2b) and I am trying to load a css file while the 'b2b' role is logged in or the administrator. I have tried the code below and I can't get it to work. Any help or suggestion would be much appreciated.
Thank you in advance.
function testCss() {
$user = wp_get_current_user();
if ( $user->roles[0]=='administrator' || $user->roles[0]=='b2b' ) {
// Add custom script.
wp_enqueue_style(
'jupiterx-child',
get_stylesheet_directory_uri() . '/assets/css/b2bstyle.css'
);
}
}
add_action('wp_enqueue_scripts','testCss', 11);
I have created a custom role (b2b) and I am trying to load a css file while the 'b2b' role is logged in or the administrator. I have tried the code below and I can't get it to work. Any help or suggestion would be much appreciated.
Thank you in advance.
function testCss() {
$user = wp_get_current_user();
if ( $user->roles[0]=='administrator' || $user->roles[0]=='b2b' ) {
// Add custom script.
wp_enqueue_style(
'jupiterx-child',
get_stylesheet_directory_uri() . '/assets/css/b2bstyle.css'
);
}
}
add_action('wp_enqueue_scripts','testCss', 11);
Share
Improve this question
asked Jul 25, 2021 at 14:20
aggzaggz
11 bronze badge
1 Answer
Reset to default 0Your if
check can result of a php notice because not logged in users have no roles and in that case $user->roles[0]
will output notice: array offest
(in short).
Aside from that, have you checked your console network to see if /assets/css/b2bstyle.css
was loaded, even if you see status 404
you are in the right direction.
Your code seems to be correct, apart from the if
check.
I would suggest doing something like this
function testCss () {
// get user
$user = wp_get_current_user();
// check if user has the following roles (administrator OR b2b)
if (in_array('administrator', $user->roles) || in_array('b2b', $user->roles)) {
wp_enqueue_style('jupiterx-child', get_stylesheet_directory_uri() . '/assets/css/b2bstyle.css');
}
}
add_action('wp_enqueue_scripts', 'testCss', 11);
If you check your console and go to network and see something like this
Click on it to reveal more information about the request, under Headers -> General, the very first line show you the request url, check to see that that is the correct one
本文标签: functionsHow to load a css file depending on the current role
版权声明:本文标题:functions - How to load a css file depending on the current role 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741422269a2377873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论