ワードプレスの記事タイトルや本文内にある全角英数字を半角英数字に自動で変換させる方法・手順を紹介しています。
何故半角に変換する必要があったのか?
ご存知だと思いますが、X(旧Twitter)には文字数制限があり、全角が1文字で計算されるのに対し半角は0.5文字で計算される為、全角を半角に変換するだけでポスト出来る文字数が増えます。
私の場合、良くワードプレスからXへ自動ポストを行なうのでこれを行なう必要がありました。
特に楽天トラベルを紹介する時に必要で、楽天トラベルって何故かサイト内にある英字/数字が全て全角なんですよね。何ならスペースも全角です。
ホテルの名前が英字で10文字の場合、半角に変換するだけで5文字節約出来ますからね。
文字数を気にしない方にはどうでも良い話だとは思いますが、全角の英数字って何かカッコ悪く見えるんだよね(笑)
私は断然半角の方が好きです(#^.^#)
function.phpに追記するだけでOK
以下二通りの変換方法を紹介していきます。
- 公開記事だけを変換する方法
- 編集画面も変換する方法
また、タイトルのみ変換する方法、記事本文のみを変換する方法、タイトルと記事本文両方変換する方法を紹介します。(全部で6パターンのコード)
ご使用テーマのfunction.phpに追記して下さい。(念の為バックアップを取ってから行なって下さい)
タイトルのみ変換する場合
公開ページだけ半角に変換するコード
/*****************************
公開ページのタイトルを半角に変換
******************************/
function convert_fullwidth_to_halfwidth_display_only_title($title) {
if (!is_admin()) { // 管理画面では変換しない
return mb_convert_kana($title, 'as', 'UTF-8');
}
return $title;
}
add_filter('the_title', 'convert_fullwidth_to_halfwidth_display_only_title');
公開ページと編集画面を半角に変換するコード
/**************************************
公開ページと編集画面のタイトルを半角に変換
***************************************/
// タイトルだけを半角変換
function convert_title_fullwidth_to_halfwidth($title) {
return mb_convert_kana($title, 'as', 'UTF-8');
}
add_filter('the_title', 'convert_title_fullwidth_to_halfwidth');
// 保存時のタイトルだけを変換
function convert_title_on_save($data, $postarr) {
if (in_array($data['post_type'], ['post', 'page'])) {
$data['post_title'] = mb_convert_kana($data['post_title'], 'as', 'UTF-8');
}
return $data;
}
add_filter('wp_insert_post_data', 'convert_title_on_save', 10, 2);
// REST API 経由でのタイトル変換
function convert_title_on_rest($post, $request) {
if ($post instanceof WP_Post && in_array($post->post_type, ['post', 'page'])) {
$post->post_title = mb_convert_kana($post->post_title, 'as', 'UTF-8');
}
return $post;
}
add_filter('rest_pre_insert_post', 'convert_title_on_rest', 10, 2);
本文のみ変換する場合
公開ページだけ半角に変換するコード
/*****************************
公開ページのタイトルを半角に変換
******************************/
function convert_fullwidth_to_halfwidth_display_only_content($content) {
if (!is_admin()) { // 管理画面では変換しない
return mb_convert_kana($content, 'as', 'UTF-8');
}
return $content;
}
add_filter('the_content', 'convert_fullwidth_to_halfwidth_display_only_content');
公開ページと編集画面を半角に変換するコード
/**************************************
公開ページと編集画面のタイトルを半角に変換
***************************************/
// 本文だけを半角変換
function convert_content_fullwidth_to_halfwidth($content) {
return mb_convert_kana($content, 'as', 'UTF-8');
}
add_filter('the_content', 'convert_content_fullwidth_to_halfwidth');
// 保存時の本文だけを変換
function convert_content_on_save($data, $postarr) {
if (in_array($data['post_type'], ['post', 'page'])) {
$data['post_content'] = mb_convert_kana($data['post_content'], 'as', 'UTF-8');
}
return $data;
}
add_filter('wp_insert_post_data', 'convert_content_on_save', 10, 2);
// REST API 経由での本文変換
function convert_content_on_rest($post, $request) {
if ($post instanceof WP_Post && in_array($post->post_type, ['post', 'page'])) {
$post->post_content = mb_convert_kana($post->post_content, 'as', 'UTF-8');
}
return $post;
}
add_filter('rest_pre_insert_post', 'convert_content_on_rest', 10, 2);
タイトルと本文を変換する場合
公開ページだけ半角に変換するコード
/**************************************
公開ページと編集画面のタイトルを半角に変換
***************************************/
function convert_fullwidth_to_halfwidth_display_only($content) {
if (!is_admin()) { // 管理画面では変換しない
return mb_convert_kana($content, 'as', 'UTF-8');
}
return $content;
}
add_filter('the_content', 'convert_fullwidth_to_halfwidth_display_only');
add_filter('the_title', 'convert_fullwidth_to_halfwidth_display_only');
公開ページと編集画面を半角に変換するコード
/**************************************
公開ページと編集画面のタイトルを半角に変換
***************************************/
function convert_fullwidth_to_halfwidth($content) {
return mb_convert_kana($content, 'as', 'UTF-8');
}
add_filter('the_content', 'convert_fullwidth_to_halfwidth');
add_filter('the_title', 'convert_fullwidth_to_halfwidth');
function convert_fullwidth_to_halfwidth_on_save($data, $postarr) {
if (in_array($data['post_type'], ['post', 'page'])) {
$data['post_title'] = mb_convert_kana($data['post_title'], 'as', 'UTF-8');
$data['post_content'] = mb_convert_kana($data['post_content'], 'as', 'UTF-8');
}
return $data;
}
add_filter('wp_insert_post_data', 'convert_fullwidth_to_halfwidth_on_save', 10, 2);
function convert_fullwidth_to_halfwidth_on_rest($post, $request) {
if ($post instanceof WP_Post && in_array($post->post_type, ['post', 'page'])) {
$post->post_title = mb_convert_kana($post->post_title, 'as', 'UTF-8');
$post->post_content = mb_convert_kana($post->post_content, 'as', 'UTF-8');
}
return $post;
}
add_filter('rest_pre_insert_post', 'convert_fullwidth_to_halfwidth_on_rest', 10, 2);
お好みのパターンを選択して実際に全角英数字(スペースも)が半角に変換されるか試してみて下さいね(^_-)-☆