この記事は最後に更新してから1年以上経過しています。
説明
投稿画面において、画像をアップロードし投稿に挿入するすると、imgタグをaタグで囲んだものが挿入される(キャプションを指定した場合はタグも)。それらの画像をLightBox(JavaScriptライブラリ)で表示する場合、aタグにrel属性を追加するのだが、その作業を毎回投稿に挿入した後で行うのがちょっと面倒。というわけで、自動的にrel属性を追加するようにしてみました。まず投稿にアップロードした画像のHTMLコードを挿入している仕組みを調べた。 画像のアップロードしている部分はiframeタグで「/wp-admin/media-upload.php」で行っており、目的のHTMLコードを挿入しているのは「/wp-admin/includes/media.php」のimage_send_to_editorフィルターであり、そのフィルターとして登録されているのが同media.phpで定義されているimage_add_caption関数だった。
aタグにrel属性を追加するには、このimage_add_caption関数と同じようなフィルター関数を定義すればいい。具体的には、自身のテーマのfunctions.phpに次の内容を記述する。
add_filter( 'image_send_to_editor', 'mytheme_image_send_to_editor' );
function mytheme_image_send_to_editor( $html, $id = '', $caption = '', $title = '', $align = '', $url = '', $size = '', $alt = '' ) {
return ( strpos( $html, 'rel=' ) === false )? str_replace( '><img src', ' rel="lightbox"<>img src', $html ): $html;
}
管理者向けのカスタマイズは、まだまだいろいろできそうですね。
最終更新 : 2011年07月29日 16:21
関連
お勧め
has_category(2012年1月17日 更新)
bool has_category( [ mixed $category = '' [ , mixed $post = null ] ] )
投稿記事がカテゴリーに属しているか調べる。
comments_template(2018年5月27日 更新)
void comments_template( string $file = '/comments.php' , bool $separate_comments = false )
投稿ページ(post)または単一ページ(page)のコメント情報を取得し、コメント表示・投稿用のテンプレートファイルを読み込んで表示する。
is_taxonomy_hierarchical(2018年5月27日 更新)
bool is_taxonomy_hierarchical( string $taxonomy )
タクソノミーに階層(親子)関係があるか調べる。
send_confirmation_on_profile_email(2018年5月27日 更新)
void send_confirmation_on_profile_email()
メールアドレス変更を確認するメールを送信する。
get_the_modified_time(2023年9月20日 更新)
string | int | false get_the_modified_time( [ string $format = '' [ , int | WP_Post $post = null ] ] )
パラメータ$formatで指定したフォーマットで投稿の更新時刻を取得する。