WordPress 管理画面の翻訳された文字列の変更

Filed under: functions.php,wordpress — kdcs @ 23年2月22日 水曜日

サイトの内容によっては管理画面内の文字列が合っていない場合があるため、翻訳された文字列を変更したい場合がある。

例:ユーザープロフィール画面の「ブログ上の表示名」を「フルネーム」に変更する

※通常、翻訳ファイルはja.poとja.moだが管理画面についてはadmin-ja.poとadmin-ja.moになるので今回の場合はadmin-ja.poからmsgidを調べる。
「ブログ上の表示名」のmsgidは「Display name publicly as」

functions.phpに記述(翻訳テキストを変える)

add_filter( 'gettext', function( $translation, $text, $domain ) {
	global $pagenow;
	if ( 'profile.php' === $pagenow || 'user-edit.php' === $pagenow ) {
		if ( 'default' === $domain ) {
			$texts = array (
				'Display name publicly as' => 'フルネーム(確認)',
			);
			if ( isset( $texts[$text] ) ) {
				$translation = $texts[$text];
			}
		}
	}
	return $translation;
}, 10, 3 );

姓と名を変更する場合

add_filter( 'gettext', function( $translation, $text, $domain ) {
	global $pagenow;
	if ( 'profile.php' === $pagenow || 'user-edit.php' === $pagenow ) {
		if ( 'default' === $domain ) {
			$texts = array (
                                'First Name' => 'あなたの名前',
				'Last Name' => 'あなたの名字',
			);
			if ( isset( $texts[$text] ) ) {
				$translation = $texts[$text];
			}
		}
	}
	return $translation;
}, 10, 3 );

cssで行う場合は「for=”display_name”」を指定する

add_action( 'admin_enqueue_scripts', function( $hook ) {
	if ( 'user-edit.php' !== $hook && 'profile.php' !== $hook && 'profile.php' !== $hook ) return;
	$style = <<<STYLE
		label[for="display_name"] {
			font-size: 0;
		}
		label[for="display_name"]:before {
			content: "フルネーム(確認)";
			font-size: 14px;
		}
STYLE;
	wp_add_inline_style( 'wp-admin', $style );
} );

サイト内検索

カテゴリー

最近の投稿

« |WordPress 管理画面の翻訳された文字列の変更| »
↑上に戻る