[英]How I can add admin css by username in 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'){
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>";
}
給定第二個錯誤
致命錯誤:未捕獲錯誤:調用未定義的 function wp_get_current_user() in /home/dh_w4i633/site.com/wp-content/plugins/me/me.php:13 堆棧跟蹤:46 堆棧跟蹤:#0 /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
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.