ダリの雑記:WEBプログラム版

タイトル・内容のフック場所【WordPress】

タイトル・内容のフック場所は、フロントエンド、管理画面で異なるので注意が必要です。

フロントエンドの場合

[PHP]

//タイトルにフックをかける(the_titleをフック)
add_filter('the_title', hoge_the_title, 1);

//内容にフックをかける(the_contentをフック)
add_filter('the_content', hoge_the_content, 1);

[/PHP]

バックエンドの場合

[PHP]

//投稿一覧のタイトル表示にフックをかける(the_postをフックし、内部で処理)
add_filter('the_post', hoge_the_post, 100);
function hoge_the_post($post){
//$post->post_titleを編集する 例:$post->post_title = htmlspecialchars($post->post_title);
return $post;
}

//編集画面のタイトル入力フィールドにフックをかける(title_edit_pre'をフック)
add_filter('title_edit_pre', hoge_the_title, 1);

//編集画面の内容フィールドにフックをかける(the_editor_contentをフック)
add_filter('the_editor_content', hoge_the_content, 1);

[/PHP]

 

単純に、the_titleやthe_contentにフックするだけでは、管理画面の方で反映してくれないので注意。

特に投稿一覧では、表示だけならthe_titleのフックで行けますが、クイック編集の方にも反映させる場合は、the_postにフックし、$post->post_titleに処理を加える必要があります。

モバイルバージョンを終了