任意の文字をタグに変換したり、削除したりする
決まった文字で囲んだ所を、別の文字に置き換えたり、装飾を付けたりするときに使います。
特定の文字に囲まれた場合の一括置換 例) **~**
functions.php
/*----------------------------------------------------
管理画面以外の記事本文を任意の内容で一括置換して表示
----------------------------------------------------*/
if ( !function_exists( 'replace_the_content' ) ){
function replace_the_content( $content ) {
$pattern = '/\*\*(.+?)\*\*/u';
$replace = '<strong style="color:red;">$1</strong>';
$content = preg_replace( $pattern, $replace, $content );
return $content;
}
if ( !is_admin() ) {
add_filter( 'the_content', 'replace_the_content', 10000 );
}
}
replace_the_content() を the_content フィルターに登録し、本文の中の **文字** を赤い太字に置き換えます(管理画面では動きません)。

