我正在嘗試為管理員添加自定義 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')
- 1 回答
- 0 關注
- 102 瀏覽
添加回答
舉報
0/150
提交
取消