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

欧比万

有人可以解释为什么下面的代码不会更改 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Microsoft Word 2007中隐藏文本格式弹出窗口(迷你工具栏)

来自分类Dev

你如何在方案中求和

来自分类Dev

你如何在java中安排?

来自分类Dev

你如何在 React 中渲染 HOC?

来自分类Dev

如何在Windows 8.1中启用管理工具?

来自分类Dev

你如何在 dotty 中使用通配符?

来自分类Dev

你如何在 Ubuntu 20.04 上休眠?

来自分类Dev

如何在Wordpress上调整标题和管理栏

来自分类Dev

jQuery工具你好世界模板

来自分类Dev

你如何在 Swift 4 中获得文本长度?

来自分类Dev

你好!如何在Quasar框架上进行分页?

来自分类Dev

你如何更改 wordpress 中一个特定链接的链接颜色?

来自分类Dev

如何在工具栏按钮中更改文本?

来自分类Dev

如何在新工具栏中更改标题文本和按钮颜色而不更改操作溢出菜单文本颜色?

来自分类Dev

如何在Wordpress中获得首页标题

来自分类Dev

你(或者你能)如何在 JOVO 的 Intents 中运行异步代码?

来自分类Dev

你如何在Java中11则启动schemagen?

来自分类Dev

你如何在 jQuery 中连接数据?

来自分类Dev

你如何在 Eigen 中做 numpy.repeat ?

来自分类Dev

你如何在haskell的函数参数中传递列表?

来自分类Dev

你如何在这个 PHP/PDO 中停止注入

来自分类Dev

你如何在 boost::python 中“从 __future__ 导入部门”?

来自分类Dev

你如何在jooq中创建多列子查询

来自分类Dev

你如何在 Folly Dynamic 中附加对象?

来自分类Dev

你如何在 Unity 中为双动作卡编码?

来自分类Dev

你如何在 ng-template 中渲染 ContentChildren 元素?

来自分类Dev

你如何在 Angular 2 中订阅 FormData 对象的数组?

来自分类Dev

你如何在laravel eloquent中查询孩子和父母

来自分类Dev

你如何在laravel eloquent中查询孩子和父母

Related 相关文章

  1. 1

    如何在Microsoft Word 2007中隐藏文本格式弹出窗口(迷你工具栏)

  2. 2

    你如何在方案中求和

  3. 3

    你如何在java中安排?

  4. 4

    你如何在 React 中渲染 HOC?

  5. 5

    如何在Windows 8.1中启用管理工具?

  6. 6

    你如何在 dotty 中使用通配符?

  7. 7

    你如何在 Ubuntu 20.04 上休眠?

  8. 8

    如何在Wordpress上调整标题和管理栏

  9. 9

    jQuery工具你好世界模板

  10. 10

    你如何在 Swift 4 中获得文本长度?

  11. 11

    你好!如何在Quasar框架上进行分页?

  12. 12

    你如何更改 wordpress 中一个特定链接的链接颜色?

  13. 13

    如何在工具栏按钮中更改文本?

  14. 14

    如何在新工具栏中更改标题文本和按钮颜色而不更改操作溢出菜单文本颜色?

  15. 15

    如何在Wordpress中获得首页标题

  16. 16

    你(或者你能)如何在 JOVO 的 Intents 中运行异步代码?

  17. 17

    你如何在Java中11则启动schemagen?

  18. 18

    你如何在 jQuery 中连接数据?

  19. 19

    你如何在 Eigen 中做 numpy.repeat ?

  20. 20

    你如何在haskell的函数参数中传递列表?

  21. 21

    你如何在这个 PHP/PDO 中停止注入

  22. 22

    你如何在 boost::python 中“从 __future__ 导入部门”?

  23. 23

    你如何在jooq中创建多列子查询

  24. 24

    你如何在 Folly Dynamic 中附加对象?

  25. 25

    你如何在 Unity 中为双动作卡编码?

  26. 26

    你如何在 ng-template 中渲染 ContentChildren 元素?

  27. 27

    你如何在 Angular 2 中订阅 FormData 对象的数组?

  28. 28

    你如何在laravel eloquent中查询孩子和父母

  29. 29

    你如何在laravel eloquent中查询孩子和父母

热门标签

归档