有人可以解释为什么下面的代码不会更改 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 操作参考(我相信这是正确的术语) - wp_after_admin_bar_render - 在functions.php文件中使用javascript更新工具栏,否则你会得到未定义的“空”错误,因为id 标题问候在DOM中不可用,如果你需要它仅使用 - admin_bar_menu - 动作参考。
我相信可以写出更好的评论,但我认为这涵盖了它最初不起作用的原因以及为改进代码、纠正错误和实现所需功能所做的工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句