WordPress 后台发布文章提示用户填写标签

之前分享了 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 &quot;&lt;script&gt;
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&gt;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&gt;1){
			  save_click_events.unshift(save_click_events.pop());
		  }
		}
	}
});
&lt;/script&gt;&quot;;
	}
}

第 9 行限定了只对普通的文章(post)才生效。第 14 行是提示内容。

来源:

https://www.wpdaxue.com/require-tags-before-publish.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?