为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 WordPress 中通过用户名添加管理 CSS

如何在 WordPress 中通过用户名添加管理 CSS

PHP
慕虎7371278 2023-09-22 16:21:10
我正在尝试为管理员添加自定义 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'){                echo "<style>#wpwrap{background-color:red;}</style>";                }    }并且工作完美,但我想将其用作用户名,而不是用户 ID有可能有吗?如果是的话请给我建议谢谢编辑后的代码1st...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>";  } }第二...给出错误<?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>";  }给定第二个错误致命错误:未捕获错误:调用 /home/dh_w4i633/site.com/wp-content/plugins/me/me.php:13 中未定义的函数 wp_get_current_user() 堆栈跟踪:#0 /home/dh_w4i633/site.com/ wp-settings.php(371): include_once() #1 /home/dh_w4i633/site.com/wp-config.php(106): require_once('/home/dh_w4i633...') #2 /home/dh_w4i633 /site.com/wp-load.php(37): require_once('/home/dh_w4i633...') #3 /home/dh_w4i633/site.com/wp-admin/admin.php(34): require_once( '/home/dh_w4i633...') #4 /home/dh_w4i633/site.com/wp-admin/plugins.php(10): require_once('/home/dh_w4i633...') #5 {main} 抛出在 /home/dh_w4i633/site.com/wp-content/plugins/me/me.php 第 13 行
查看完整描述

1 回答

?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

<?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>";


  }

?>

编辑:


编辑中的第二个代码会给您带来错误,因为您始终运行它而不是将其附加到挂钩,因此会出现错误。


您编辑中的第一个代码似乎是正确的,所以让我们坚持这一点。它为所有用户(包括不需要的用户)更改 css 的原因是因为 OR||运算符。您需要将其替换为 AND&&运算符。你的情况将变成:


if(is_admin() && $username != 'xyz' && $username != 'abc')


查看完整回答
反对 回复 2023-09-22
  • 1 回答
  • 0 关注
  • 45 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信