亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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 關注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號