之前分享了 WordPress 后台发布文章时提示用户选择分类,今天要分享的是 WordPress 后台发布文章提示用户填写标签,如果用户没有添加标签就点击发布/保存文章,就会弹出信息,提示他添加标签。将下面的代码添加到主题的functions.php 即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * WordPress 后台发布文章提示用户填写标签 * https://www.wpdaxue.com/require-tags-before-publish.html */ add_action('admin_footer-post.php', 'require_tags_before_publish'); add_action('admin_footer-post-new.php', 'require_tags_before_publish'); function require_tags_before_publish(){ global $post_type; if($post_type=='post'){ // 只对文章(post)生效 echo "<script> jQuery(function($){ $('#publish, #save-post').click(function(e){ if($('#post_tag .tagchecklist span').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>"; } } |
/** * WordPress 后台发布文章提示用户填写标签 * https://www.wpdaxue.com/require-tags-before-publish.html */ add_action('admin_footer-post.php', 'require_tags_before_publish'); add_action('admin_footer-post-new.php', 'require_tags_before_publish'); function require_tags_before_publish(){ global $post_type; if($post_type=='post'){ // 只对文章(post)生效 echo "<script> jQuery(function($){ $('#publish, #save-post').click(function(e){ if($('#post_tag .tagchecklist span').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>"; } }
第 9 行限定了只对普通的文章(post)才生效。第 14 行是提示内容。
本文WordPress 后台发布文章提示用户填写标签到此结束。当你被失败拥抱时,成功可能正在一边等着吻你。小编再次感谢大家对我们的支持!