LoginSignup
10
12

More than 5 years have passed since last update.

WordPressの投稿作成画面で空欄ならアラートを出す

Last updated at Posted at 2016-06-28

仕様

  • タイトルの入力を必須にしたい

空欄があったらアラートを出す

mytheme_post_edit_required.php
<?php
add_action( 'admin_head-post-new.php', 'mytheme_post_edit_required' ); // 新規投稿画面でフック
add_action( 'admin_head-post.php', 'mytheme_post_edit_required' );     // 投稿編集画面でフック
function mytheme_post_edit_required() {
?>
<script type="text/javascript">
jQuery(document).ready(function($){
  if( 'post' == $('#post_type').val() || 'page' == $('#post_type').val() ){ // post_type 判定。例は投稿と固定ページ。カスタム投稿タイプは適宜追加
    $("#post").submit(function(e){ // 更新あるいは下書き保存を押したとき
      if('' == $('#title').val()) { // タイトル欄の場合
        alert('タイトルを入力してください!');
        $('.spinner').hide(); // spinnerアイコンを隠す
        $('#publish').removeClass('button-primary-disabled'); // #publishからクラス削除
        $('#title').focus(); // 入力欄にフォーカス
        return false;
      }
    });
  }
});
</script>
<?php
}

応用で「アイキャッチ画像を必須化」ってのもできます。
https://github.com/jaws-ug/jawsdays2016/blob/master/wp-content/themes/jawsdays2016/inc/extras.php#L47-L72

10
12
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
10
12