
先日、WordPressのダッシュボード(管理画面)の上部に、ちょっとしたメッセージを表示させたくなりました。
機能のイメージとしては、WordPressインストール時にデフォルトでインストールされているプラグインの「Hello Dolly」みたいなイメージです。
こんな感じで、「管理画面上部に何かしらのメッセージを表示したい」という感じです。
Hello Dollyは、ジャズトランペット奏者でありシンガーでもあるルイ・アームストロングの代表曲の1つ「Hello Dolly」の歌詞の一節を単に表示するプラグインです。
今回は、「Hello Dollyと同等の機能を持ち、歌詞は表示させずに任意の文字列やHTMLをダッシュボードトップに表示するWordpressカスタマイズ方法」の紹介です。
目次
テーマカスタマイズの主な手順

カスタマイズの手順はこんな感じです。
- functions.phpにPHPコードのコピペ
- テーマフォルダ直下にdashboard-message.txtファイルの作成
- dashboard-message.txtファイルに一言メッセージを記入
基本的に、functions.phpにPHPコードをコピペした後に、テキストファイルを作成してメッセージを記入するだけのカスタマイズです。
functions.phpにPHPコードのコピペ
テーマ(子テーマ)のfunctions.phpに以下のコードを貼り付けます。
//メッセージの取得
if ( !function_exists( 'dashboard_message_get_text' ) ):
function dashboard_message_get_text() {
//メッセージファイルを取得
$filename = get_template_directory() . '/dashboard-message.txt';
if (file_exists($filename)) {
$messages = file_get_contents( $filename );
//改行で分ける
$messages = explode( "\n", $messages );
//一行だけを表示する
return wptexturize( $messages[ rand( 0, count( $messages ) - 1 ) ] );
}
}
endif;
//メッセージHTMLの出力
add_action( 'admin_notices', 'generate_dashboard_message' );
if ( !function_exists( 'generate_dashboard_message' ) ):
function generate_dashboard_message() {
$chosen = dashboard_message_get_text();
if ($chosen) {
$lang = '';
if ( 'en_' !== substr( get_user_locale(), 0, 3 ) ) {
$lang = ' lang="en"';
}
printf(
'<p id="dashboard-message"><span class="screen-reader-text">%s </span><span dir="ltr"%s>%s</span></p>',
'ダッシュボードメッセージ:',
$lang,
$chosen
);
}
}
endif;
//メッセージエリアのCSS出力
add_action( 'admin_head', 'dashboard_message_css' );
if ( !function_exists( 'dashboard_message_css' ) ):
function dashboard_message_css() {
echo "
<style type='text/css'>
#dashboard-message {
float: right;
padding: 5px 10px;
margin: 0;
font-size: 12px;
line-height: 1.6666;
}
.rtl #dashboard-message {
float: left;
}
.block-editor-page #dashboard-message {
display: none;
}
@media screen and (max-width: 782px) {
#dashboard-message,
.rtl #dashboard-message {
float: none;
padding-left: 0;
padding-right: 0;
}
}
</style>
";
}
endif;
上記のコードは、Toshiyuki HondaさんのHello Musouを参考に書かせていただきました。
テーマフォルダ直下にテキストファイルの作成
次に、「テーマフォルダ直下(functions.phpと同じ階層)」に「dashboard-message.txt」というファイルを作成します。
テキストファイルに一言メッセージを記入
最後に「dashboard-message.txt」ファイルを開いて、メッセージを以下のように記入します。内容は何でも良いです。
新型コロナ対策:<a href="https://corona.go.jp/prevention/" target="_blank" rel="noopener">新型コロナウイルス(COVID-19) 感染しない・広げないために|内閣官房</a> 新型コロナ対策:換気の悪い「<a href="https://corona.go.jp/prevention/pdf/cluster2.pdf" target="_blank" rel="noopener">密閉空間</a>」は避けましょう 新型コロナ対策:多くの人が集まる「<a href="https://corona.go.jp/prevention/pdf/cluster2.pdf" target="_blank" rel="noopener">密集空間</a>」を避けましょう 新型コロナ対策:近距離での会話や発声をする「<a href="https://corona.go.jp/prevention/pdf/cluster2.pdf" target="_blank" rel="noopener">密接場面</a>」を避けましょう 新型コロナ対策の基本は「<a href="https://corona.go.jp/prevention/pdf/tearai.pdf" target="_blank" rel="noopener">手洗い</a>」や「<a href="https://corona.go.jp/prevention/pdf/seki.pdf" target="_blank" rel="noopener">マスクの着用を含む咳エチケット</a>」です 新型コロナ対策スローガン:<a href="https://twitter.com/search?q=%23%E8%B7%9D%E9%9B%A2%E3%82%92%E3%81%82%E3%81%91%E3%82%88%E3%81%86&src=typed_query" target="_blank" rel="noopener">#距離をあけよう</a>、<a href="https://twitter.com/hashtag/KEEPDISTANCE?src=hashtag_click" target="_blank" rel="noopener">#KeepDistance</a> 新型コロナ対策スローガン:<a href="https://twitter.com/hashtag/%E3%81%86%E3%81%A1%E3%81%A7%E9%81%8E%E3%81%94%E3%81%9D%E3%81%86?src=hashtag_click" target="_blank" rel="noopener">#うちで過ごそう</a>、<a href="https://twitter.com/hashtag/StayHome?src=hashtag_click">#StayHome</a> 新型コロナ情報:<a href="https://gisanddata.maps.arcgis.com/apps/opsdashboard/index.html#/bda7594740fd40299423467b48e9ecf6" target="_blank" rel="noopener">国別新型コロナウイルス感染者数マップ</a> 新型コロナ情報:<a href="https://jagjapan.maps.arcgis.com/apps/opsdashboard/index.html#/641eba7fef234a47880e1e1dc4de85ce" target="_blank" rel="noopener">都道府県別新型コロナウイルス感染者数マップ</a> 新型コロナ情報:<a href="https://newsdigest.jp/pages/coronavirus/" target="_blank" rel="noopener">新型コロナウイルスの最新感染状況マップ</a> 新型コロナ情報:<a href="https://www.stopcovid19.jp/" target="_blank" rel="noopener">都道府県別の空き感染症病床数</a> 新型コロナ情報:<a href="https://stopcovid19.metro.tokyo.lg.jp/" target="_blank" rel="noopener">東京都内の最新感染動向</a> 新型コロナ情報:<a href="https://corona.go.jp/" target="_blank" rel="noopener">緊急事態宣言の成果~人流の減少率~</a> 新型コロナ税制:<a href="https://www.mof.go.jp/tax_policy/keizaitaisaku.html" target="_blank" rel="noopener">新型コロナウイルス感染症緊急経済対策における税制上の措置(案)</a> 新型コロナ補助:<a href="https://www3.nhk.or.jp/news/special/coronavirus/management/" target="_blank" rel="noopener">「こんなときはどんな支援が?」 助成金や補助金の情報</a>
一行につき一つのメッセージを記入します。
HTMLタグを記入した場合は、HTMLとして出力されます。
動作確認
最後にWordPress管理画面を開いて、画面右上にメッセージが出ているかを確認します。
通常は「dashboard-message.txt」に入力したテキストが一行ごとに以下のように表示されます。
大抵のWordPress管理画面なら画面遷移をする毎に一言メッセージが表示されます。
表示順は、単なるランダムです。
まとめ
こんな感じで、WordPress管理者・編集者向けの一言メッセージを表示することができます。
この機能の使いどころとしては、こんな感じでしょうか。
- 多編集者が在籍するサイトで管理者が皆にお知らせしたいことがあるとき
- 忘れっぽい人が自分用のメモなどを残しておく
- TODOとしての利用
一言メッセージの変更は、テキストファイルを開いてテキストベースで変更することが可能です。
なので比較的簡単に通知メッセージを変更することが出来るかと思います。



