wordpressはそのまま使っても高機能で使いやいCMSですが、自己流にカスマイズすることでさらに機能的になります。
例えば、管理サイトが多くなってきて管理画面にもファビコンを付けたいとか、レスポンシブでサイトを構築した場合はサムネイル画像のwidthとheightを削除したい場合もあります。
管理画面にファビコンをつける
2 | function admin_favicon() { |
3 | echo '<link rel="shortcut icon" type="image/x-icon" href="' .get_bloginfo( 'template_url' ). '/images/favicon.ico" />' ; |
5 | add_action( 'admin_head' , 'admin_favicon' ); |
※このケースでは、予め使用中テーマのimagesフォルダ内にfavicon.icoを入れておく必要があります。
コメントのHTML表記とメール、ウェブサイト入力欄、「メールアドレスが公開~」を削除
2 | function my_special_comment_after( $args ){ |
3 | $args [ 'comment_notes_after' ] = '' ; |
6 | add_filter( "comment_form_defaults" , "my_special_comment_after" ); |
9 | function my_comment_form_remove( $arg ) { |
14 | add_filter( 'comment_form_default_fields' , 'my_comment_form_remove' ); |
17 | function my_comment_form_before( $defaults ){ |
18 | $defaults [ 'comment_notes_before' ] = '' ; |
21 | add_filter( "comment_form_defaults" , "my_comment_form_before" ); |
※設定→ディスカッションから「」のチェックを外すしておく
画像からwidthとheightを削除する
2 | add_filter( 'post_thumbnail_html' , 'remove_width_and_height_attribute' , 10 ); |
3 | add_filter( 'image_send_to_editor' , 'remove_width_and_height_attribute' , 10 ); |
4 | function remove_width_and_height_attribute( $html ) { |
5 | return preg_replace( '/(height|width)="\d*"\s/' , "" , $html ); |
レスポンシブで画像を%指定したときに便利です。
管理画面の投稿一覧にアイキチャッチを載せる
2 | add_theme_support( 'post-thumbnails' ); |
3 | function manage_posts_columns( $columns ) { |
4 | $columns [ 'thumbnail' ] = __( 'Thumbnail' ); |
7 | function add_column( $column_name , $post_id ) { |
9 | if ( 'thumbnail' == $column_name ) { |
10 | $thum = get_the_post_thumbnail( $post_id , array (50,50), 'thumbnail' ); |
13 | if ( isset( $stitle ) && $stitle ) { |
14 | echo attribute_escape( $stitle ); |
15 | } else if ( isset( $thum ) && $thum ) { |
21 | add_filter( 'manage_posts_columns' , 'manage_posts_columns' ); |
22 | add_action( 'manage_posts_custom_column' , 'add_column' , 10, 2 ); |
更新する時にわかりやすくて便利です
投稿から画像挿入した時に自動で作成される画像リンクを削除する
1 | add_filter( 'the_content' , 'attachment_image_link_remove_filter' ); |
2 | function attachment_image_link_remove_filter( $content ) { |
3 | $content = preg_replace( array ( '{<a(.*?)(wp-att|wp-content/uploads)[^>]*><img}' , '{ wp-image-[0-9]*" /></a>}' ), array ( '<img' , '" />' ), $content ); |
これはwp-content/uploads配下の画像ファイル全てに適応されるので、アフィリエイト画像などを画像挿入で入れるとリンク切れになる場合があります
コメント入力欄