Php 您如何更改我的帐户名称;“你好”;wordpress管理工具栏中的文本
有人能解释一下为什么下面的代码没有改变“我的帐户”节点吗 我已经尝试了各种各样的教程和指南,我可以在WordPress4.8中找到它们来更改“我的帐户”节点文本,但我所做的一切似乎都不起作用。我在两个不同的站点上尝试了这些站点的不同代码(包括下面的代码),具有两个不同的主题,行为是相同的-管理工具栏中的目标节点没有更改 我留下了一个注释掉的变体,它应该简单地替换函数底部的节点,以防有人告诉我为什么它不起作用。我已经翻遍了Wordpress的抄本,试图弄清楚这一点,但是没有用 我也尝试过将优先级设置为999,但也没有效果Php 您如何更改我的帐户名称;“你好”;wordpress管理工具栏中的文本,php,wordpress,Php,Wordpress,有人能解释一下为什么下面的代码没有改变“我的帐户”节点吗 我已经尝试了各种各样的教程和指南,我可以在WordPress4.8中找到它们来更改“我的帐户”节点文本,但我所做的一切似乎都不起作用。我在两个不同的站点上尝试了这些站点的不同代码(包括下面的代码),具有两个不同的主题,行为是相同的-管理工具栏中的目标节点没有更改 我留下了一个注释掉的变体,它应该简单地替换函数底部的节点,以防有人告诉我为什么它不起作用。我已经翻遍了Wordpress的抄本,试图弄清楚这一点,但是没有用 我也尝试过将优先级设
/* --- 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;
}
/*--更改管理栏的问候语--*/
添加操作(“管理栏菜单”、“更新管理栏用户节点”,250);
函数更新\管理\栏\用户\节点($wp\管理\栏){
$user\u id=get\u current\u user\u id();
$current_user=wp_get_current_user();
$profile\u url=get\u edit\u profile\u url($user\u id);
如果(!$user\u id)
返回;
如果(当前用户可以(“读取”)){
$profile\u url=get\u edit\u profile\u url($user\u id);
}elseif(is_multisite()){
$profile_url=get_dashboard_url($user_id,'profile.php');
}否则{
$profile_url=false;
}
$avatar=get_avatar($user_id,26);
$msgtext=fancy_greeting_text();
$newtitle=sprintf(uuuu(“%1$s,%2$s”),$msgtext,“.$current_user->display_name.”);
$class=empty($avatar)?“”:“有avatar”;
//删除当前“我的帐户”节点
$wp_admin_bar->删除_节点(“我的帐户”);
//将节点与更新一起添加回去
$wp_管理_栏->添加_节点(数组(
“id”=>“我的帐户”,
“父级”=>“顶级中学”,
“title”=>$newtitle.$avatar,
'href'=>$profile\u url,
'meta'=>数组(
“类”=>$class,
),
) );
//让我们继续并将用户网站添加到子菜单(如果有)
//如果必须删除上面的节点,则需要重建用户操作菜单的其余部分
$my_account=$wp_admin_bar->get_节点('my account');
如果(!empty($current\u user->user\u url)&&$my\u account){
$wp\u管理\u栏->添加\u节点(数组)(
“父项”=>“用户操作”,
“id'=>“用户url”,
“title'=>”。“(我的网站”)。”,
'href'=>esc\u url($current\u user->user\u url)
) );
}
//$my_account=$wp_admin_bar->get_节点(“my-account”);
//$msgtext=fancy_greeting_text();
//$newtitle=str_replace('Howdy',$msgtext,$my_account->title);
//$args=数组(
//“id”=>“我的帐户”,
//“标题”=>$newtitle,
// );
//$wp\u admin\u bar->添加节点($args);
}
函数fancy_greeting_text(){
//日期默认时区设置(“美国/丹佛”);
$date=日期('d-m');
$hour=日期('G');
交换机(日期){
案例“01-01”:
$message='新年快乐';
打破
案例“25-12”:
$message='圣诞快乐';
打破
违约:
//$message='Welcome';
//$message=“登录身份”;
如果($hour>=5&&$hour=12&&$hour=19 | | |$hours因此,经过几天的反复拨弄,我终于弄明白了发生了什么,虽然这是一件简单的蠢事,但我还是抓住机会在过程中稍微清理了一下代码。因此,这不起作用的真正原因是一个旨在操纵日期的函数客户端的nd time在我从其他地方的文章中粘贴的代码部分中有一个卷曲的撇号,当时我正试图找出如何让用户有时间到服务器定制一天中的某个时间的问候语。应该注意的是,有问题的代码在这个函数之前,并且影响了它之后的一切
最终,我选择了一种更简单、稍有不同的方法来使用javascript实现我的目标……生成的代码发布在下面
我也知道这段代码可能有一些地方可以改进,但由于我使用的是一个较旧的托管帐户,简单性和向后兼容性是一个因素。我还修改了fancy_greeting_text函数,并将其作为在客户端禁用javascript或因其他原因无法运行javascript时的备用功能ntf已更改为将问候语包装在span标记中,以便javascript稍后可以访问和更改它。另外,已更改的内容是,删除“我的帐户”节点的行是不必要的,它似乎对使用所需更改呈现工具栏没有影响
无论如何,更新后的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
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/*---更改管理栏的问候语--*/
添加操作(“管理栏菜单”、“更新管理栏用户节点”,250);
函数更新\管理\栏\用户\节点($wp\管理\栏){
$user\u id=get\u current\u user\u id();
$current_user=wp_get_current_user();
$profile\u url=get\u edit\u profile\u url($user\u id);
如果(!$user\u id)
返回;
如果(当前用户可以(“读取”)){
$profile\u url=get\u edit\u profile\u url($user\u id);
}elseif(is_multisite()){
$profile_url=get_dashboard_url($user_id,'profile.php');
}否则{
$profile_url=false;
}
$avatar=get_avatar($user_id,26);
$greeting=fancy_greeting_text();
/*令牌:[%1:问候语文本][%2:当前用户的显示名称]*/
$newtitle=sprintf(“%1$s,%2$s”)、“.$greeting.”、“.$current\u user->display\u name.”