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

之前分享了 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 行是提示内容。

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号