Php 如何在wordpress中通过用户名添加管理员css
我正在尝试为管理员添加自定义css,如下所示Php 如何在wordpress中通过用户名添加管理员css,php,css,wordpress,Php,Css,Wordpress,我正在尝试为管理员添加自定义css,如下所示 add_action('admin_head', 'hey_custom_admin_css'); function hey_custom_admin_css() { global $current_user; $user_id = get_current_user_id(); if(is_admin() && $user_id != '1'){
add_action('admin_head', 'hey_custom_admin_css');
function hey_custom_admin_css() {
global $current_user;
$user_id = get_current_user_id();
if(is_admin() && $user_id != '1'){
echo "<style>#wpwrap{background-color:red;}</style>";
}
}
add_action('admin_head','hey_custom_admin_css');
函数hey\u custom\u admin\u css(){
全局$当前用户;
$user\u id=get\u current\u user\u id();
if(is_admin()&&$user_id!=“1”){
回声“#wpwrap{背景色:红色;}”;
}
}
工作非常完美,但我想将其用于用户名,而不是用户ID
有可能吗?如果是,请给我一些建议
谢谢
编辑代码 第一。。。Css正在工作,但适用于所有用户。包括代码中列出的
<?php
/**
* Plugin Name: My Plugin
* Plugin URI: https://google.com
* Description: Me
* Version: 1.0
* Author: Plugin
* Author URI: https://google.com
*/
add_action('admin_head', 'hey_custom_admin_css');
function hey_custom_admin_css() {
global $current_user;
wp_get_current_user();
$username = $current_user->user_login;
if(is_admin() && ($username != 'xyz' || $username != 'abc')){
echo "
<style>
#wpwrap{background-color:red;}
</style>";
} }
编辑:
编辑中的第二个代码会给您一个错误,因为您一直在运行它,而不是将它附加到钩子上,因此会出现错误
您编辑的第一个代码似乎是正确的,所以让我们坚持下去。它更改所有用户(包括不需要的用户)的css的原因是因为OR|
运算符。您需要将其替换为AND&&
运算符。您的情况将变为:
如果(is_admin()&&&$username!='xyz'&&&$username!='abc')
是的,一种方法是在我的编辑中添加另一个条件,如上所示。你能发布新的完整代码吗?请正确设置格式您可能希望尝试编辑您的初始帖子,以便下次包含代码。很少有人愿意下载文件。不管怎样,我认为你错过了第二个条件的括号。它应该是这样的:if(is_admin()&($username=='username'.\124;$username=='other_username'))
你可以把它作为:if(is_admin()&&$username=='username'.\124;$username=='other_username')
你能把你的新代码作为编辑添加到你的初始帖子中吗?你能不能加上这个错误?工作得很好。。。非常感谢你。。我不知道该怎么感谢你。我不知道该怎么说。衷心感谢你。非常感谢你。。。再次感谢:D
<?php
/**
* Plugin Name: My Plugin
* Plugin URI: https://google.com
* Description: Me
* Version: 1.0
* Author: Plugin
* Author URI: https://google.com
*/
global $current_user;
wp_get_current_user(); //Error on this line
$username = $current_user->user_login;
if(is_admin() && ($username != 'xyz' || $username != 'abc')){
echo "
<style>
#wpwrap{background-color:red;}
</style>";
}
<?php
global $current_user;
wp_get_current_user();
$username = $current_user->user_login;
if(is_admin() && ($username == 'username' || $username == 'another_username')){
echo "<style>#wpwrap{background-color:red;}</style>";
}
?>