Skip to content

Instantly share code, notes, and snippets.

@gatespace
Last active July 18, 2017 04:32
Show Gist options
  • Save gatespace/cc05016b62eb140a5d48b35db0815604 to your computer and use it in GitHub Desktop.
Save gatespace/cc05016b62eb140a5d48b35db0815604 to your computer and use it in GitHub Desktop.
WordPress カスタム分類(タクソノミー)にデフォルトでチェックをいれたい ref: http://qiita.com/gatespace/items/ca6fedb03b10ff8b746c
<?php
// カスタム分類(タクソノミー)の book の スラッグ blue がチェックされてるかどうか
add_action( 'admin_head-post-new.php', 'book_term_post_edit_required' ); // 新規投稿画面でフック
add_action( 'admin_head-post.php', 'book_term_post_edit_required' ); // 投稿編集画面でフック
function book_term_post_edit_required() {
// book タクソノミーの blue タームのIDを取得
$book_term = get_term_by( 'slug', 'blue', 'book' );
$book_term_id = $book_term->term_id;
// ここでIDの有無をちゃんとチェックした方がいいのだけどね。
?>
<script type="text/javascript">
jQuery(document).ready(function($){
// load した時
var term_check = $('#taxonomy-book input:checked').length;
if ( term_check == 0 ){
$('#in-book-<?php echo $book_term_id ?>').prop('checked',true);
$('#in-popular-book-<?php echo $book_term_id ?>').prop('checked',true);
}
if( 'post' == $('#post_type').val() ) {
$("#post").submit(function(e){ // 更新あるいは下書き保存を押したとき
term_check = $('#taxonomy-book input:checked').length;
if ( term_check == 0 ){
alert('Bookにチェックを入れてください');
$('.spinner').hide(); // spinnerアイコンを隠す
$('#publish').removeClass('button-primary-disabled'); // #publishからクラス削除
$('#in-book-<?php echo $book_term_id ?>').prop('checked',true);
return false;
}
});
}
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment