黄聪:WordPress 后台发布文章时提示用户选择分类

很多用户在后台发布文章,常常会忘记选择分类,所以很有必要添加一个提醒功能,如果没有选择分类,点击发布时,就显示一个提示信息。要实现这个功能,只要将下面的代码添加到主题的 functions.php 即可:

/**
 * WordPress 发布文章前必须选择分类
 * http://www.wpdaxue.com/choose-a-category-before-publish.html
 */
add_action('admin_footer-post.php', 'choose_a_category_before_publish');
add_action('admin_footer-post-new.php', 'choose_a_category_before_publish');
function choose_a_category_before_publish(){
    global $post_type;
    if($post_type=='post'){
        echo "<script>
jQuery(function($){
    $('#publish, #save-post').click(function(e){
        if($('#taxonomy-category input:checked').length==0){
            alert('抱歉,发布文章前,请选择一个分类');
            e.stopImmediatePropagation();
            return false;
        }else{
            return true;
        }
    });
    var publish_click_events = $('#publish').data('events').click;
    if(publish_click_events){
        if(publish_click_events.length>1){
            publish_click_events.unshift(publish_click_events.pop());
        }
    }
    if($('#save-post').data('events') != null){
        var save_click_events = $('#save-post').data('events').click;
        if(save_click_events){
          if(save_click_events.length>1){
              save_click_events.unshift(save_click_events.pop());
          }
        }
    }
});
</script>";
    }
}
原文地址:https://www.cnblogs.com/huangcong/p/4349399.html