黄聪

论SEO对人类的重要性,请看我的博客:hcsem.com

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

很多用户在后台发布文章,常常会忘记选择分类,所以很有必要添加一个提醒功能,如果没有选择分类,点击发布时,就显示一个提示信息。要实现这个功能,只要将下面的代码添加到主题的 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>";
    }
}

 

posted on 2015-03-19 08:34  黄聪  阅读(381)  评论(0编辑  收藏  举报