WordPress 投稿の必須入力アラート

Filed under: functions.php,wordpress — kdcs @ 18年1月9日 火曜日

デフォルトでは未入力でも下書き保存・公開ができてしまうので
必須入力の項目を指定し、未入力の場合はアラートを出す
function.php内に記述

デフォルトの投稿

function post_edit_required() {
?>
<script>
jQuery(function($) {
  if( 'post' == $('#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;
      }
    });
  }
});
</script>
<?php
}
add_action( 'admin_head-post-new.php', 'post_edit_required' );
add_action( 'admin_head-post.php', 'post_edit_required' );

カスタム投稿タイプのみの場合。(カスタム投稿タイプが「information」)
5行目

if ( 'information' == $('#post_type').val() )

カスタム投稿のカテゴリーはタクソノミーが任意の記述なので、
カスタムのタクソノミー(例:xxxx_category)に変更する必要がある

      // カテゴリー カスタム投稿
      if ( $('#taxonomy-xxxx_category input:checked').length < 1 ) {
        alert('カテゴリーを選択してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#taxonomy-xxxx_category a[href="#category-all"]').focus();
        return false;
      }

デフォルトの投稿とカスタム投稿を併用する場合(カスタム投稿タイプが「information」)

// デフォルト(投稿)・information(カスタム投稿)必須入力のアラート-----------------------------
function post_edit_required() {
?>
<script>
jQuery(function($) {
  if( 'post' == $('#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 ( $('#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( 'information' == $('#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 ( $('#taxonomy-information_category input:checked').length < 1 ) {
        alert('カテゴリーを選択してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#taxonomy-information_category a[href="#category-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' );

サイト内検索

カテゴリー

最近の投稿

« |WordPress 投稿の必須入力アラート| »
↑上に戻る