content のfilter をカスタムフィールドにも

情報を追記している場合はありますが、古い情報を訂正はしていませんので、公開年月日を参照してください。プラグイン・タグ、いずれもワードプレス・PHPのバージョン等によって動作しない場合もあります。

投稿の関連画像としてカスタムフィールド(Advanced Custom Fields使用)でアップロードした複数の画像の拡大表示にPrettyPhoto(http://wordpress.org/plugins/rt-prettyphoto/)を使ったのだけど、このプラグインでは、よくあるライトボックスプラグインのように「自動」の設定がない。

カスタムフィールドでアップロードする分は、どちらにしろテーマに表示タグを書くから構わないのだけど、投稿やカスタムフィールドのエディターでリンクを付けた画像もPrettyPhoto で開きたい、ってことで…

以下をfunctionsに *フィールド名がvisualの場合

add_filter('the_content', 'add_prettyPhoto');
function add_prettyPhoto($content) {
       global $post;
       $pattern ="/<a(.*?)href=('|")(.*?).(bmp|gif|jpeg|jpg|png)('|")(.*?)>/i";
       $replacement = '<a$1href=$2$3.$4$5 rel="prettyPhoto[post]" title="'.$post->post_title.'"$6>';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
}

function add_prettyPhoto_acf($value, $post_id, $field )
{
    $value = apply_filters('the_content',$value);
    return $value;
}
add_filter('acf/load_value/name=visual', 'add_prettyPhoto_acf', 10, 3);

その他のAdvanced Custom Fields に関する記事一覧

Advanced Custom Fields に関する記事一覧ページへ