实际上,所有插件将代码都等同于将代码片段添加到functions.php中,functions.php代码片段可以生成插件,然后插件可以自然地集成到functions.php中。插件主要易于管理,但也增加了静态资源请求的数量。如果您知道如何处理这些请求,就不必使用插件来优化站点。WordPress网站发布文章时如何防止发布重复标题的文章?

主要的修改地方是:js转为script标签内容,等于删除一个请求,翻译也被保存,用中文代替。把下面的代码复制并粘贴到你的主题的functions.php文件中。
/ **
*发表文章时禁止与已存在的标题相重复
*从插件修改:重复标题验证
*说明:此插件帮助,不允许发布Duplicate Title。
*作者:哈桑·莫瓦希德
*修订者:INLOJV
*版本:1.0
* /
//发表文章页面,前端抓取标题并使用AJAX发送请求
add_action(’admin_print_footer_scripts’,’duplicate_titles_enqueue_scripts’,100);
函数repeat_titles_enqueue_scripts(){
?>
<脚本>
jQuery(函数($){
函数checkTitleAjax(title,id,post_type){
var data = {
动作:“ title_checks”,
post_title:标题,
post_type:post_type,
post_id:id
};
$ .post(ajaxurl,data,function(response){
$(’#message’)。remove();
$(’#poststuff’)。prepend('<div id =” message” class =“”在-h2以下更新“> <p>’+响应+'</ p> </ div>’));
});
};
$(’#title’)。change(function(){
var title = $(’#title’)。val();
var id = $(’#post_ID’)。val();
var post_type = $(’#post_type’)。val();
checkTitleAjax(title,id,post_type);
});
});
</ script>
<?php
}
//接收前端ajax参数
add_action(’wp_ajax_title_checks’,’duplicate_title_checks_callback’);
函数repeat_title_checks_callback(){
全局$ wpdb;
$ title = $ _POST [‘post_title’];
$ post_id = $ _POST [‘post_id’];
$ titles =“从$ wpdb-> posts中选择post_title,而post_status =’publish’AND post_type =’post’
AND post_title ='{$ title}’AND ID!= {$ post_id}“;
$ results = $ wpdb-> get_results($ titles);
if($ results){
回显“ <span style =’color:red’>”。_(’此标题已存在,请换一个标题!’,”)。” </ span>”;
}其他{
echo'<span style =” color:green”>’._(’恭喜,此标题未与其他文章标题重复!’,”)。'</ span>’;
}
死();
}
//检测标题和避免同名文章更新草稿
add_action(’publish_post’,’duplicate_titles_wallfa_bc’);
函数repeat_titles_wallfa_bc($ post){
全局$ wpdb;
$ title = $ _POST [‘post_title’];
$ post_id = $ post;
$ wtitles =“从$ wpdb-> posts中选择post_title,post_status =’publish’AND post_type =’post’
AND post_title ='{$ title}’AND ID!= {$ post_id}”;
$ wresults = $ wpdb-> get_results($ wtitles);
如果($ wresults){
$ wpdb-> update($ wpdb-> posts,array(’post_status’=>
’draft’),array(’ID’=> $ post)));
$ arr_params = array(’message’=>’10’,’wallfaerror’=>’1’);
$ location = add_query_arg($ arr_params,get_edit_post_link($ post,’url’));
wp_redirect($ location);
出口 ;
}
}
///文章提交更新后的提示
add_action(’admin_notices’,’not_published_error_notice’);
函数not_published_error_notice(){
if(isset($ _ GET [‘wallfaerror’])== 1){
?>
<div class =“已更新”>
<p style =’color:red’> <?php _e(’貌似已经存在相同标题的文章,如果您使用了文章别名作为固定链接,则可以通过修改此处的固定链接来使标题不再重复!” ,”)?> </ p>
</ div>
<?php
}
}
//补充自动保存
add_action(’wp_print_scripts’,’disable_autosave’);
函数disable_autosave(){
wp_deregister_script(’autosave’);
}
Your article helped me a lot, is there any more related content? Thanks! https://accounts.binance.com/vi/register?ref=T7KCZASX
I am an investor of gate io, I have consulted a lot of information, I hope to upgrade my investment strategy with a new model. Your article creation ideas have given me a lot of inspiration, but I still have some doubts. I wonder if you can help me? Thanks.