[英]how do you change the my-account title “Howdy” text in wordpress admin toolbar
有人可以解釋一下下面的代碼為什么不更改my-account節點嗎?
我嘗試了每一個教程和指南的變體,發現它們可以更改Wordpress 4.8中的“我的帳戶”節點文本,但我似乎無濟於事。 我已經嘗試在具有2個不同主題的2個不同站點上從這些站點(包括下面的代碼)修改代碼,並且行為相同-管理工具欄中的目標節點沒有更改。
我留下了一個注釋掉的變體,應該簡單地替換函數底部的節點,以防有人可以告訴我為什么它不起作用。 我對Wordpress Codex進行了搜索,試圖弄清楚這一點,但無濟於事。
我也嘗試將優先級設置為999,但這也沒有影響。
/* --- change the greeting for the admin bar --- */
add_action( 'admin_bar_menu', 'update_admin_bar_user_node', 250 );
function update_admin_bar_user_node( $wp_admin_bar ) {
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$profile_url = get_edit_profile_url( $user_id );
if ( ! $user_id )
return;
if ( current_user_can( 'read' ) ) {
$profile_url = get_edit_profile_url( $user_id );
} elseif ( is_multisite() ) {
$profile_url = get_dashboard_url( $user_id, 'profile.php' );
} else {
$profile_url = false;
}
$avatar = get_avatar( $user_id, 26);
$msgtext = fancy_greeting_text();
$newtitle = sprintf( __( '%1$s, %2$s' ), $msgtext, '<span class="display-name">' . $current_user->display_name . '</span>' );
$class = empty( $avatar ) ? '' : 'with-avatar';
// remove the current my-account node
$wp_admin_bar->remove_node( 'my-account' );
// add the node back with the updates
$wp_admin_bar->add_node( array(
'id' => 'my-account',
'parent' => 'top-secondary',
'title' => $newtitle . $avatar,
'href' => $profile_url,
'meta' => array(
'class' => $class,
),
) );
// lets go ahead and add the users website to the sub-menu if they have one
// will need to rebuild the rest of the user-actions menu if we have to remove the node above
$my_account = $wp_admin_bar->get_node( 'my-account' );
if( ! empty( $current_user->user_url ) && $my_account ){
$wp_admin_bar->add_node( array(
'parent' => 'user-actions',
'id' => 'user-url',
'title' => '<span class="user-url">' . __( 'My Website' ) . '</span>',
'href' => esc_url( $current_user->user_url )
) );
}
// $my_account = $wp_admin_bar->get_node('my-account');
// $msgtext = fancy_greeting_text();
// $newtitle = str_replace( 'Howdy', $msgtext, $my_account->title );
// $args = array(
// 'id' => 'my-account',
// 'title' => $newtitle,
// );
// $wp_admin_bar->add_node( $args );
}
function fancy_greeting_text() {
//date_default_timezone_set('America/Denver');
$date = date('d-m');
$hour = date('G');
switch($date) {
case '01-01':
$message = 'Happy New Year';
break;
case '25-12':
$message = 'Merry Christmas';
break;
default:
//$message = 'Welcome';
//$message = "Logged in as";
if ( $hour >= 5 && $hour <= 11 ) {
$message = "Good morning";
} else if ( $hour >= 12 && $hour <= 18 ) {
$message = "Good afternoon";
} else if ( $hour >= 19 || $hours <= 4 ) {
$message = "Good evening";
}
}
return $message;
}
因此,經過幾天的反復試驗,我終於弄清楚了發生了什么,盡管這很簡單,但我還是借此機會整理了一下代碼。 因此,此功能不起作用的真正原因是一個旨在操縱來自客戶端的日期和時間的函數,在我試圖從其他地方的文章中粘貼的一段代碼中,當試圖弄清楚如何使用戶有時間使用時,在該代碼段中出現了撇號服務器自定義一天中的問候語。 可能應該注意的是,該函數之前令人討厭的代碼段,並影響了此函數之后的所有內容。
最終,我選擇了一種更簡單且略有不同的方法來使用javascript來實現我的目標...結果代碼如下。
我也知道可能在某些地方可以改進此代碼,但是由於我使用的是較舊的托管帳戶,因此簡單性和向后兼容性是一個因素。 我還修改了fancy_greeting_text函數,並將其保留為備用,以防客戶端禁用javascript或由於其他原因而無法運行。 sprintf已更改為將問候語包裝在span標簽中,以便javascript以后可以訪問和更改它。 進行了其他更改,刪除my-account節點的行是不必要的,它似乎對渲染具有所需更改的工具欄沒有影響。
無論如何,有效的更新后的functions.php代碼(包括基於客戶端的“假日”和“一天中的時間”問候)如下:
//-----------------------------------------------------------------------------
/* --- change the greeting for the admin bar --- */
add_action( 'admin_bar_menu', 'update_admin_bar_user_node', 250 );
function update_admin_bar_user_node( $wp_admin_bar ) {
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$profile_url = get_edit_profile_url( $user_id );
if ( ! $user_id )
return;
if ( current_user_can( 'read' ) ) {
$profile_url = get_edit_profile_url( $user_id );
} elseif ( is_multisite() ) {
$profile_url = get_dashboard_url( $user_id, 'profile.php' );
} else {
$profile_url = false;
}
$avatar = get_avatar( $user_id, 26);
$greeting = fancy_greeting_text();
/* tokens: [%1: greeting text] [%2: current user's display name] */
$newtitle = sprintf( __( '%1$s, %2$s' ), '<span id="title-greeting" class="greeting">' . $greeting . '</span>', '<span class="display-name">' . $current_user->display_name . '</span>' );
$class = empty( $avatar ) ? '' : 'with-avatar';
// update the node with the changes
$wp_admin_bar->add_node( array(
'id' => 'my-account',
'parent' => 'top-secondary',
'title' => $newtitle . $avatar,
'href' => $profile_url,
'meta' => array(
'class' => $class,
),
) );
// Add the users website/link to the user-actions sub-menu if they have one
$my_account = $wp_admin_bar->get_node( 'my-account' );
if( ! empty( $current_user->user_url ) && $my_account ){
$wp_admin_bar->add_node( array(
'parent' => 'user-actions',
'id' => 'user-url',
'title' => '<span class="user-url">' . __( 'My Website' ) . '</span>',
'href' => esc_url( $current_user->user_url )
) );
}
}
function fancy_greeting_text() {
/* -- greeting message based on server values - fallback -- */
if ( !date_default_timezone_get() ) {
//set a default timezone
date_default_timezone_set('America/Chicago');
}
$date = date('d-m');
$hour = date('G');
switch($date) {
case '01-01':
$message = 'Happy New Year';
break;
case '25-12':
$message = 'Merry Christmas';
break;
default:
$message = 'Welcome';
}
//debug//$message.= " (".$hour.")"; // view the hour variable used to determine greeting
return $message;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/* --- Display custom Time of Day Greeting in Wordpress Admin Toolbar --- */
add_action( 'wp_after_admin_bar_render', 'custom_title_greeting', 9999 );
function custom_title_greeting() {
?>
<script type=text/javascript>
var now = new Date();
var mm = now.getMonth()+1;//January is 0!`
var dd = now.getDate();
var hr = now.getHours();
var holidate = dd+'-'+mm;
//console.log('hr->'+hr);
switch (holidate) {
case '01-01':
$greeting = 'Happy New Year';
break;
case '25-12':
$greeting = 'Merry Christmas';
break;
default:
if ( hr >= 5 && hr <= 11 ) {
$greeting = 'Good Morning';
} else if ( hr >= 12 && hr <= 18 ) {
$greeting = 'Good Afternoon';
} else if ( hr >= 19 && hr <= 4 ) {
$greeting = 'Good Evening';
} else {
$greeting = 'Welcome';
}
}
// update the existing fallback greeting with new client based greeting
document.getElementById('title-greeting').innerHTML = $greeting;
</script>
<?php
}
//-----------------------------------------------------------------------------
對於像我這樣剛接觸Wordpress的人,我也應該指出,由於只有在呈現頁面本身的結尾附近才呈現管理工具欄,因此您必須使用以下Wordpress Action Reference(我相信這是正確的術語) -wp_after_admin_bar_render-使用來自functions.php文件中的javascript更新工具欄,否則您將得到未定義的“空”錯誤,因為ID 標題問候在DOM中不可用,如果您需要僅使用-admin_bar_menu-操作參考。
我相信可以寫出更好的評論,但是我認為這涵蓋了為什么它最初不起作用的原因,以及為改進代碼,更正錯誤和實現所需功能所做的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.