【问题标题】:How I can add admin css by username in wordpress如何在 wordpress 中通过用户名添加管理员 css
【发布时间】:2020-08-08 07:17:42
【问题描述】:

我正在尝试为管理员添加自定义 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

有可能吗?如果是,请给我建议

谢谢


修改后的代码

第一个... 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} 在第 13 行的 /home/dh_w4i633/site.com/wp-content/plugins/me/me.php 中抛出

【问题讨论】:

    标签: php css wordpress


    【解决方案1】:
    <?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 &amp;&amp; 运算符。您的情况将变为:

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

    【讨论】:

    • 是的,一种方法是在我的编辑中添加另一个条件,如上所示。
    • 您可以发布您的新完整代码吗?请正确格式化
    • 下次您可能想尝试编辑您的初始帖子以包含代码。您很少会找到愿意下载文件的人。无论如何,我认为你错过了第二个条件的括号。它应该是这样的:if(is_admin() &amp;&amp; ($username == 'username' || $username == 'another_username')) 你有它:if(is_admin() &amp;&amp; $username == 'username' || $username == 'another_username')
    • 您可以将新代码添加到您的初始帖子中作为编辑吗?您能否同时添加错误?
    • 工作得很好......非常感谢你......我不知道我该如何感谢你。我不明白。衷心感谢。非常感谢你……再次感谢:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    相关资源
    最近更新 更多