LoginSignup
0
1

More than 3 years have passed since last update.

WordPressで投稿内容の任意の項目を必須化する

Last updated at Posted at 2020-12-15

ワードプレスのテーマtwentytwentyを基盤に、投稿内容の任意の項目を必須にする実装をしたので、備忘録的にこの記事を残す。

環境情報

PHP:version 7.3.12
WordPress:version 5.5.3
WPテーマ:twentytwenty

作業

プラグインを使えば簡単にできることかもしれないが、練習がてらどうにかプラグイン無しで実装できないものかと思い見つけたやり方です。

※ポストタイプ名が blog の場合

functions.php
// 投稿時の必須項目の設定
function post_edit_required() {
?>
    <script type="text/javascript">
        jQuery(function($) {
            if( 'blog' == $('#post_type').val() ) {
                $('#post').submit(function(e) {
                    // タイトル
                    if ( '' == $('#title').val() ) {
                        alert('タイトルを入力してください');
                        $('.spinner').css('visibility', 'hidden');
                        $('#publish').removeClass('button-primary-disabled');
                        $('#title').focus();
                        return false;
                    }
                    // コンテンツ(エディタ)
                    if ( $('.wp-editor-area').val().length < 1 ) {
                        alert('コンテンツを入力してください');
                        $('.spinner').css('visibility', 'hidden');
                        $('#publish').removeClass('button-primary-disabled');
                        return false;
                    }
                    // 抜粋
                    if ( '' == $('#excerpt').val() ) {
                        alert('抜粋を入力してください');
                        $('.spinner').css('visibility', 'hidden');
                        $('#publish').removeClass('button-primary-disabled');
                        $('#excerpt').focus();
                        return false;
                    }
                    // カテゴリー
                    if ( $('#taxonomy-category input:checked').length < 1 ) {
                        alert('カテゴリーを選択してください');
                        $('.spinner').css('visibility', 'hidden');
                        $('#publish').removeClass('button-primary-disabled');
                        $('#taxonomy-category a[href="#category-all"]').focus();
                        return false;
                    }
                    // タグ
                    if ( $('#tagsdiv-post_tag .tagchecklist span').length < 1 ) {
                        alert('タグを選択してください');
                        $('.spinner').css('visibility', 'hidden');
                        $('#publish').removeClass('button-primary-disabled');
                        $('#new-tag-post_tag').focus();
                        return false;
                    }
                    // アイキャッチ
                    if ( $('#set-post-thumbnail img').length < 1 ) {
                        alert('アイキャッチ画像を設定してください');
                        $('.spinner').css('visibility', 'hidden');
                        $('#publish').removeClass('button-primary-disabled');
                        $('#set-post-thumbnail').focus();
                        return false;
                    }
                    // タクソノミー(カテゴリー)
                    if ( $('#taxonomy-カテゴリー名 input:checked').length < 1 ) {
                        alert('カテゴリーを選択してください');
                        $('.spinner').css('visibility', 'hidden');
                        $('#publish').removeClass('button-primary-disabled');
                        $('#taxonomy-カテゴリー名 a[href="#カテゴリー名-all"]').focus();
                        return false;
                    }

                });
            }
        });
    </script>
<?php
}
add_action( 'admin_head-post-new.php', 'post_edit_required' );
add_action( 'admin_head-post.php', 'post_edit_required' );

※使用される際は、必須化したくない項目の箇所をコメントアウトしてご利用ください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1