简体   繁体   English

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

[英]how do you change the my-account title “Howdy” text in wordpress admin toolbar

Could someone please explain why the code below does not change the my-account node? 有人可以解释一下下面的代码为什么不更改my-account节点吗?

I have tried variations of every tutorial and guide that I can find to change the 'my-account' node text in Wordpress 4.8 and nothing that I do seems to work. 我尝试了每一个教程和指南的变体,发现它们可以更改Wordpress 4.8中的“我的帐户”节点文本,但我似乎无济于事。 I have tried variations of code from these sites (including the code below) on 2 different sites with 2 different themes and the behavior is the same - there is no change to the targeted node(s) in the admin toolbar. 我已经尝试在具有2个不同主题的2个不同站点上从这些站点(包括下面的代码)修改代码,并且行为相同-管理工具栏中的目标节点没有更改。

I left a commented out variation that should simply replace the node at the foot of the function in case someone can tell me why that did not work. 我留下了一个注释掉的变体,应该简单地替换函数底部的节点,以防有人可以告诉我为什么它不起作用。 I've scoured the Wordpress Codex trying to figure this out as well, but to no avail. 我对Wordpress Codex进行了搜索,试图弄清楚这一点,但无济于事。

I have also tried setting the priority to 999, but that had no affect as well. 我也尝试将优先级设置为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;
}

So after days of fiddling on and off, I finally figured out what was happening, and while it was something simple-stupid, I took the opportunity to clean up the code just a tad in the process. 因此,经过几天的反复试验,我终于弄清楚了发生了什么,尽管这很简单,但我还是借此机会整理了一下代码。 So the real reason this did not work was a function intended to manipulate the date and time from the client had a curly apostrophe in a section of code that I had pasted from an article elsewhere while trying to figure out how to get the users time to the server to customize the greeting for time of day. 因此,此功能不起作用的真正原因是一个旨在操纵来自客户端的日期和时间的函数,在我试图从其他地方的文章中粘贴的一段代码中,当试图弄清楚如何使用户有时间使用时,在该代码段中出现了撇号服务器自定义一天中的问候语。 It should probably be noted the offending piece of code was BEFORE this function and affecting everything after it. 可能应该注意的是,该函数之前令人讨厌的代码段,并影响了此函数之后的所有内容。

Ultimately I opted for a simpler and slightly different approach to accomplishing my goals using javascript... the resulting code is posted below. 最终,我选择了一种更简单且略有不同的方法来使用javascript来实现我的目标...结果代码如下。

I also know there are probably areas where this code could be improved, but since I am using an older hosting account, simplicity and backward compatibility are a factor. 我也知道可能在某些地方可以改进此代码,但是由于我使用的是较旧的托管帐户,因此简单性和向后兼容性是一个因素。 I also modified the fancy_greeting_text function and kept it as a fallback in the event javascript is disabled on the client or it fails to run for some other reason. 我还修改了fancy_greeting_text函数,并将其保留为备用,以防客户端禁用javascript或由于其他原因而无法运行。 The sprintf was changed to wrap the greeting in a span tag so the javascript could later access and change it. sprintf已更改为将问候语包装在span标签中,以便javascript以后可以访问和更改它。 Something else that is changed, the line to remove the my-account node was unnecessary, it seemed to have no affect on rendering the toolbar with my desired changes. 进行了其他更改,删除my-account节点的行是不必要的,它似乎对渲染具有所需更改的工具栏没有影响。

Anyway, the updated functions.php code that worked, including the client based Holiday and Time of Day greeting is as follows: 无论如何,有效的更新后的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
}
//-----------------------------------------------------------------------------

For those who are new to Wordpress, like myself, I should also probably point out that since the admin toolbar is not rendered until near the end of the page itself being rendered, you MUST use the following Wordpress Action Reference (I believe that is the correct term) - wp_after_admin_bar_render - to update the toolbar using javascript from within the functions.php file, otherwise you will get undefined "null" errors as the id title-greeting would not be available in the DOM at the time it is needed if you only used the - admin_bar_menu - action reference. 对于像我这样刚接触Wordpress的人,我也应该指出,由于只有在呈现页面本身的结尾附近才呈现管理工具栏,因此您必须使用以下Wordpress Action Reference(我相信这是正确的术语) -wp_after_admin_bar_render-使用来自functions.php文件中的javascript更新工具栏,否则您将得到未定义的“空”错误,因为ID 标题问候在DOM中不可用,如果您需要仅使用-admin_bar_menu-操作参考。

I'm sure a better commentary could be written, but I think this covers the reasons why it did not work at first and what was done to improve the code, correct the errors and accomplish the desired functionality. 我相信可以写出更好的评论,但是我认为这涵盖了为什么它最初不起作用的原因,以及为改进代码,更正错误和实现所需功能所做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 wp-admin 中移动 WooCommerce my-account? - How to move WooCommerce my-account inside wp-admin? 如何更改 WooCommerce“我的账户/订单”按钮点击排序 - How to change sorting in WooCommerce "my-account/orders" on button click 如何在 WooCommerce /my-account/ 中编辑文本 - How can I edit the text in WooCommerce /my-account/ 我如何在Woocommerce我的帐户/编辑地址中更改TEXT - How can i change TEXT in Woocommerce my-account/edit-address 您如何更改 WooCommerce 我的帐户中的订阅文本? - How do you change subscription's text in WooCommerce my account? WordPress:如果用户角色是“旅行代理”,则重定向到“我的帐户” - Wordpress: If user role is 'travel agent' redirect to 'my-account' 更改我的帐户/密码丢失/页面上的Woocommerce密码丢失文本,无需覆盖模板 - Change Woocommerce Lost Password text on my-account/lost-password/ page without template override 如何在 WooCommerce /my-account/downloads/ 中的下载表中添加一列 - How to add a column to the downloads table in WooCommerce /my-account/downloads/ Woocommerce-如何在单独的页面上显示订单详细信息(我的帐户) - Woocommerce - How to show Order details (my-account) on a separate page Woocommerce 如何在我的帐户页面上重定向自定义端点 - Woocommerce how to redirect a custom end point on my-account page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM