繁体   English   中英

如何在wordpress管理工具栏中更改我的帐户标题“ Howdy”文本

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM