wordpressデフォルト項目が空の場合にアラートを表示する
WordPressはタイトルや、カテゴリーなどデフォルト項目が空でも投稿することが出来る。
それら項目を必須にしたい場合、下記をfunction.phpに記載する
// 必須にしたい項目が空の場合、アラートを表示
function post_edit_required() {
?>
<script type="text/javascript">
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' );
参考サイト
-
前の記事
ユーザー権限別での管理画面メニューの非表示の仕方 2018.10.17
-
次の記事
foreachの間をカンマ[ , ]で区切る 2018.11.08