/*
* WordPressの投稿作成画面で必須項目を作る(空欄ならJavaScriptのアラート)
*/
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;
}
if($("#taxonomy-category input:checked").length < 1 ) { // カテゴリーがチェックされているかどうか。条件を要確認。普通は設定したカテゴリーになるから要らない
alert('カテゴリーを選択してください');
$('.spinner').hide();
$('#publish').removeClass('button-primary-disabled');
$('#taxonomy-category').focus();
return false;
}
if( $("#set-post-thumbnail img").length < 1 ) { // アイキャッチ画像
alert('アイキャッチ画像を設定してください!');
$('.spinner').hide();
$('#publish').removeClass('button-primary-disabled');
$('#set-post-thumbnail').focus();
return false;
}
});
}
});
</script>
<?php
}