之前分享了 WordPress 后台发布文章时提示用户选择分类,今天要分享的是 WordPress 后台发布文章提示用户填写标签,如果用户没有添加标签就点击发布/保存文章,就会弹出信息,提示他添加标签。将下面的代码添加到主题的functions.php 即可:
/**
* WordPress 后台<a href="https://www.wpdaxue.com/tag/%e5%8f%91%e5%b8%83%e6%96%87%e7%ab%a0" title="查看与【发布文章】相关的文章" target="_blank" rel="noopener">发布文章</a>提示用户填写标签
* 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 后台<a href="https://www.wpdaxue.com/tag/%e5%8f%91%e5%b8%83%e6%96%87%e7%ab%a0" title="查看与【发布文章】相关的文章" target="_blank" rel="noopener">发布文章</a>提示用户填写标签
* 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 行是提示内容。
来源:
https://www.wpdaxue.com/require-tags-before-publish.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32412.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57