誰かが、以下のコードがmy-accountノードを変更しない理由を説明してください。WordPressの管理ツールバーのマイアカウントのタイトル「Howdy」のテキストを変更するには
私はすべてのチュートリアルのバリエーションを試してみました。私はWordpress 4.8で 'my-account'ノードのテキストを変更することができます。私は、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;
}
この回答はあなたに役立つかもしれません。 https://wordpress.stackexchange.com/questions/96869/edit-specific-nodes-in-wp-admin-bar – Difster
ありがとうございますが、残念ながら私はどこにも導かれませんでした...回答は実際には間違っています。既存のノードを変更したり、新しいノードを追加するだけでも、ツールバー全体を再構築する必要はありません。私は最終的になぜ私のコードがうまくいかないのかを理解しました。そして、その過程で、私が行ったことを達成するためのより単純な解決策が出てきました。 – Obewan