他者が編集中の投稿の編集禁止
他者が編集中に投稿を編集しようとして失敗したことがあるので編集禁止にしています。
コード
// -------------------------------- // 他者が編集中の投稿の編集禁止 // -------------------------------- function hk_functions_override_post_lock( $override ) { return false; } add_filter( 'override_post_lock', 'hk_functions_override_post_lock' );
「ブラウザー内のこの投稿の下書きが・・・」非表示
この表示は、公開済み・下書の投稿を途中まで修正して保存しない場合に表示されます。
一般ユーザーが使い方を間違えると困るので非表示にします。
JavaScript で表示・非表示を操作する領域のHTMLを削除しています。
コード
// -------------------------------- // 「ブラウザー内のこの投稿の下書きが・・・」非表示 // -------------------------------- function hk_functions_in_admin_footer() { remove_action( 'admin_footer', '_local_storage_notice' ); return; } add_action( 'in_admin_footer', 'hk_functions_in_admin_footer' );
カテゴリ名表示
投稿のカテゴリ名を、「投稿の編集」とタイトル入力欄の間に表示します。
投稿のカテゴリを一つに限定していることが前提です。
コード
// -------------------------------- // カテゴリ名表示 // -------------------------------- function hk_functions_edit_form_top( $post ) { if ( $post->post_type != 'post' ) return; $cat_id = 0; if ( $post->ID ) { $cats = get_the_category( $post->ID ); if ( $cats ) $cat_id = $cats[0]->cat_ID; } if ( $cat_id ) $cat_title = get_cat_name( $cat_id ); else $cat_title = '(未設定)'; echo '<p>カテゴリ:'.htmlspecialchars( $cat_title )."</p>\n"; return; } add_action( 'edit_form_top', 'hk_functions_edit_form_top' );
サブタイトル入力欄表示
投稿の作成でサブタイトルを設定したい場合があります。タイトル入力欄の下にサブタイトル入力欄を作成します。
サブタイトル入力欄の設置は投稿のみにしています。固定ページに設置することも可能です。
コード
// -------------------------------- // サブタイトル入力欄表示 // -------------------------------- function hk_functions_edit_form_sub_title( $post ) { // post に限定 $post_type = $post->post_type; if ( $post_type != 'post' ) return; // post_id $post_id = $post->ID; // サブタイトル $sub_title = get_post_meta( $post_id, 'sub_title', true ); echo '<p>サブタイトル:<br />'."\n"; echo '<input type="text" name="hk_sub_title" size="30" value="'.htmlspecialchars( $sub_title ).'" class="hk_width100" />'."\n"; echo '</p>'."\n"; return; } add_action( 'edit_form_before_permalink', 'hk_functions_edit_form_sub_title' );
ファイルアップロード機能追加
投稿にファイルを関連づけたい場合があります。たとえばイベントの開催案内ファイルです。カテゴリ毎に、ファイルを関連づける可能性があるかどうか設定しています。カテゴリの“説明”をこのカテゴリタイプの判定に利用しています。ファイルを関連づける可能性があるカテゴリの“説明”には、あらかじめ'date'と書き込んでいます。カテゴリの“説明”を“テーマ”で使用している場合には、この方法は使えません。関数 hk_func_get_cat_type は、“共通関数”に記載します。
通常の投稿作成画面にファイルアップロード機能はありませんので、ファイルアップロード機能を付ける必要があります。
formタグに enctype属性を付加しています。
コード
// -------------------------------- // ファイルアップロード機能追加 // -------------------------------- function hk_functions_post_edit_form_tag( $post ) { // 通常の新規投稿を除外 if ( ! isset( $_GET['post'] ) ) return; $post_id = $_GET['post']; // 'post'以外を除外 if ( $post->post_type != 'post' ) return; // カテゴリ $cats = get_the_category( $post_id ); if ( empty( $cats ) ) return; $cat_id = $cats[0]->cat_ID; // カテゴリタイプ $cat_type = hk_func_get_cat_type( $cat_id ); if ( $cat_type == 'date' ) echo ' enctype="multipart/form-data"'; return; } add_action( 'post_edit_form_tag', 'hk_functions_post_edit_form_tag' );
テキストボタン・文字数・自動保存メッセージ・最後の編集・検索非表示 テキストエディタ用フォント
html編集ができないユーザーが使いやすいように、ビジュアル・テキスト切り替えのテキストボタンを非表示にしています。
テキストエディターで入力すると \ 記号がバックスラッシュで表示されます。使いにくいのでテキストエディターのフォントを変更しています。
文字数は、不要なので非表示にしています。
自動保存メッセージは、別途自動保存自体を無効にしており、不要なエラーメッセージを表示したくないため非表示にしています。
最後の編集は、不要なので非表示にしています。
“メディアを追加”の「検索」は、不要なので非表示にしています。
コード
// -------------------------------- // スタイル // テキストボタン非表示(HTML編集不可能ユーザー) // テキストエディタ用フォント // 文字数 非表示 // 自動保存メッセージ・最後の編集 非表示 // メディアを追加(JavaScript)「検索」非表示 // -------------------------------- function hk_functions_admin_head() { global $pagenow; // スタイル記述 echo '<style type="text/css">'."\n"; // 投稿作成 if (( $pagenow == 'post.php' ) || ( $pagenow == 'post-new.php' )) { // テキストボタン非表示(HTML編集不可能ユーザー) $user_id = get_current_user_id(); $can_html = get_user_meta( $user_id, 'hk_can_html', true ); if ( $can_html != 'yes' ) echo ' .wp-editor-tabs .switch-html { display: none !important; }'."\n"; // テキストエディタ用フォント echo ' .wp-editor-wrap .wp-editor-container textarea.wp-editor-area { font-family: "MS ゴシック", Helvetica, Arial, sans-serif; }'."\n"; // 文字数 非表示 echo ' #wp-word-count { display: none !important; }'."\n"; // 自動保存メッセージ・最後の編集 非表示 echo ' .autosave-info { display: none !important; }'."\n"; // メディアを追加(JavaScript)「検索」非表示 echo ' #media-search-input { display: none !important; }'."\n"; } echo '</style>'."\n"; return; } add_action( 'admin_head', 'hk_functions_admin_head' );
ビジュアルエディタのスタイル修正
WordPressのビジュアルエディタの表示を実際の表示に近いようにしたい場合、“テーマ”の editor-style.css のファイルを修正します(プラグインの機能ではありません)。
次のような場合を例にとります。“テーマ”で違いがあると思います。注意しなければならないのは、修正したファイルをアップロードしても、すぐには、反映されないことです。ログアウトした後、インターネットエクスプローラ(IE)のキャッシュを完全にクリアし、ログインし直す必要があります。
・フォントを変更
・表(TABLE)のスタイル変更
この場合、次のようなコードをファイルの末尾に追加します。
コード
/* - FONT - */ body, pre, code, kbd, samp, var, input[type="text"], textarea, table, td, th { font-family: "MS ゴシック", Helvetica, Arial, sans-serif; } /* - TABLE - */ table { border-style: none; } tr th, td { border: 1px solid #a9a9a9 !important; padding: 6px; padding: 0.428571428rem; } tr th { text-align: center; } td { vertical-align: top; }
「集中執筆モード」非表示
不要なので非表示。
コード
// -------------------------------- // 「集中執筆モード」非表示 // -------------------------------- function hk_functions_wp_editor_expand( $expand ) { return false; } add_filter( 'wp_editor_expand', 'hk_functions_wp_editor_expand' );
tiny_mce メニュー変更
HTML編集可能かどうかで表示するメニューを変えます。
コード
// -------------------------------- // tiny_mce メニュー変更 // メニュー1段目 // -------------------------------- function hk_functions_mce_buttons( $mce_buttons ) { $user_id = get_current_user_id(); $can_html = get_user_meta( $user_id, 'hk_can_html', true ); // HTML編集可能者 if ( $can_html == 'yes' ) $mce_buttons = array( 'bold', 'italic', 'bullist', 'numlist', 'alignleft', 'aligncenter', 'alignright', 'link', 'unlink', ); // HTML編集不可能者 else $mce_buttons = array( 'bold', 'italic', 'alignleft', 'aligncenter', 'alignright', 'underline', 'forecolor', 'removeformat', 'charmap', 'undo', 'redo', ); return $mce_buttons; } add_filter( 'mce_buttons', 'hk_functions_mce_buttons' ); // -------------------------------- // tiny_mce メニュー変更 // メニュー2段目 // -------------------------------- function hk_functions_mce_buttons_2( $mce_buttons_2 ) { $user_id = get_current_user_id(); $can_html = get_user_meta( $user_id, 'hk_can_html', true ); // HTML編集可能者 if ( $can_html == 'yes' ) $mce_buttons_2 = array( 'formatselect', 'underline', 'forecolor', 'removeformat', 'charmap', 'undo', 'redo', ); // HTML編集不可能者 else $mce_buttons_2 = array(); return $mce_buttons_2; } add_filter( 'mce_buttons_2', 'hk_functions_mce_buttons_2' );
「text メニュー」変更
一般ユーザーが使いやすいようにしています。
コード
// -------------------------------- // 「text メニュー」変更 // -------------------------------- function hk_functions_quicktags_settings( $qtInit ) { $qtInit['buttons'] = 'strong,em,link,ul,ol,li'; return $qtInit; } add_filter( 'quicktags_settings', 'hk_functions_quicktags_settings' );
テンプレート読み込み
定型投稿のテンプレートを読み込みます。テンプレートは、カテゴリ毎に作成しておきます。利用するカテゴリの投稿として、タイトルを“テンプレート”、'post_status'を'private(非公開)'としてあらかじめ作成しておきます。
利用するフックは、'the_editor_content'です。しかし、このフックは、パラメータの情報不足で editor_id がわかりません。この editor_id を取得するため、フック'wp_editor_settings'を利用しています。
editor_id は少なくとも次の4種類あります。
'content'
'replycontent'
'pressthis'
'attachment_content'
対象とするのは、'content'だけですが、これは少なくとも次の二つに分類されます。
投稿編集
コメント編集
対象とするのは、投稿編集だけです。この二つの違いは、パラメータ'tinymce'が true かどうかで判定できます。対象とするのは、'tinymce'が true の場合です。
'tinymce'が true かどうかは、フィルター'format_for_editor'の有無のチェックで行うことができます。フィルター'format_for_editor'は、'tinymce'が true の場合に存在します。
フック'the_editor_content'には、フィルター'format_for_editor'があります。このフィルターで関数 htmlspecialchars を実行します。そのため、このフィルターより前にテンプレート読み込みを行う必要があり、テンプレート読み込み用フィルターの優先順位を9にしています。
なお、フックを設定しています。このフックは自作の他のプラグインで利用しています。
コード
// エディターパラメータ $hk_editor_param = array(); // -------------------------------- // エディターパラメータ取得 // editor_id // -------------------------------- function hk_functions_editor_settings( $settings, $editor_id ) { global $hk_editor_param; // editor_id $hk_editor_param['editor_id'] = $editor_id; // $settings['tinymce'] if ( $settings['tinymce'] == false ) $hk_editor_param['tinymce'] = false; else $hk_editor_param['tinymce'] = true; return $settings; } add_filter( 'wp_editor_settings', 'hk_functions_editor_settings', 10, 2 ); // -------------------------------- // テンプレート読み込み // フック設定 // -------------------------------- function hk_functions_editor_content( $content, $default_editor ) { global $hk_editor_param; // editor_id チェック if ( $hk_editor_param['editor_id'] != 'content' ) return $content; // tinymce チェック if ( ! $hk_editor_param['tinymce'] ) return $content; // フィルター有無チェック(念のため) if ( has_filter( 'the_editor_content', 'format_for_editor' ) === false ) return $content; // テンプレート読み込み function hk_read_template( $content ) { if ( $content != '' ) return $content; global $post; // 'post'以外を除外 if ( $post->post_type != 'post' ) return $content; // 序文、テンプレートを除外 $post_title = $post->post_title; if (( $post_title == '序文' ) || ( $post_title == 'テンプレート' )) return $content; // check post_id $post_id = $post->ID; if ( empty( $post_id ) ) return $content; // check cat_id $cats = get_the_category( $post_id ); if ( empty( $cats ) ) return $content; $cat_id = $cats[0]->cat_ID; if ( empty( $cat_id ) ) return $content; // $post 退避 $post_org = $post; // テンプレート取得 $args = array( 'category__in' => $cat_id, 'post_status' => 'private', 'order' => 'DESC', 'orderby' => 'date', 'posts_per_page' => -1, ); $template_id = 0; $my_query = new WP_Query( $args ); if ( ! $my_query->have_posts() ) return $content; while ( $my_query->have_posts() ) { $my_query->the_post(); $my_post = get_post(); if ( isset( $my_post->post_title ) ) { if ( $my_post->post_title == 'テンプレート' ) { $template_id = $my_post->ID; break; } } } wp_reset_postdata(); // $post 復元 $post = $post_org; // テンプレート無し if ( $template_id == 0 ) return $content; // テンプレート取得 $template = get_post( $template_id ); $content = $template->post_content; $content = trim( $content ); return $content; } $content = hk_read_template( $content ); // フック設定 $content = apply_filters( 'hk_func_editor_content', $content, $default_editor ); return $content; } add_filter( 'the_editor_content', 'hk_functions_editor_content', 9, 2 );
このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。