WordPressのfunction.phpで追加しているもの
ということで最近メモ代わりに記事にしています。
あいすBALSTです。
WordPressではFunction.phpにてもーこれはまーかゆいところに手が届きまくる魔法のPHPなのですが、如何せん半角スペースとか見えないトラブルにもなる、Wordpressが初心者向けのサービスではないという象徴的な魔法の言語です。
そんな僕が書いていてコピペで繋いでるようなものではありますが一部メモ代わりに書いておこうと思います。
昔はこの量の数倍の量を書いていましたが必要無くなったり消え去ったりで、忘れて書いてないものもありますが今のところあまり問題にはなってないので多分大丈夫です 相対リンクとか相対リンクとか相対パスとかそうたいりんくとか・・・・・
投稿のスラッグを自動連番で採番する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// スラッグ連番付番 function incliment_slug($slug) { global $post; // 固定ページの面倒は見ません if (($slug) || ($post->post_type == 'page')) { return $slug; } global $wpdb; $slug = $wpdb->get_var("select max(cast(post_name as unsigned)) from {$wpdb->posts} where post_type = 'post' and post_status not in ('object', 'attachment', 'inherit') and post_name regexp '^[0-9]+$'"); $slug ++; return $slug; } // フィルターフック add_filter('editable_slug', 'incliment_slug'); |
これのおかげで以前の記事のスラッグの番号に+1して番号を振ってくれるので、日付で管理してない人にはいいかも。
画像タグの不要な属性とかサイズのタグを自動で排除
1 2 3 4 5 6 7 8 9 10 11 12 |
// 【管理画面】メディアを追加で挿入されるimgタグから不要な属性を削除 add_filter('image_send_to_editor', 'remove_image_attribute', 10); add_filter('post_thumbnail_html', 'remove_image_attribute', 10); function remove_image_attribute($html){ $html = preg_replace('/(width|height)="\d*"\s/', '', $html); // width と heifht を削除 $html = preg_replace('/class=[\'"]([^\'"]+)[\'"]/i', '', $html); // class を削除 $html = preg_replace('/title=[\'"]([^\'"]+)[\'"]/i', '', $html); // title を削除 $html = preg_replace('/<a href=".+">/', '', $html); // a タグを削除 $html = preg_replace('/<\/a>/', '', $html); // a の閉じタグのを削除 return $html; } |
ごちゃごちゃするリンクは見にくいので気分的に消したいのです。
SEO的にはちゃんと説明文とかつけておくのがいいそうです。
ただ、サイズとかはアップロード時に揃えているので指定は不要なのです。(個人的に)
テキストエディターを読みやすく見やすく
1 2 3 4 5 6 7 8 9 10 |
// text-editor style function change_editor_font(){ echo'<style type="text/css"> textarea#content.wp-editor-area { font-family:Meiryo; font-size:14px; } </style>'; } |
ビジュアルエディターではなくテキストエディタを見やすくするのに文字サイズとフォントを変更しています。
と言っても、ただのメイリオですが、これだけでいい。
もっと凝りたい人はfont-familyを変更してあげてください。