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 行是提示內容。

發佈留言