阻止发布同名文章。
通常文章都是通过外部链接,站长很忙,有时会发布同样的标题。这是为了给这个字符串代码进行简单的预注册。
注意:建议在传统编辑器下使用。
发布文章时,可以对文章标题进行查重。
将这个函数放在主题根目录下的functions
.php
文件中。
tag
发表文章页面,前端抓取标题并使用 AJAX
发送请求
add_action
( 'admin_print_footer_scripts
', 'dupli
cat
接收前端 ajax
参数
检测后端标题并且避免同名文章更新草稿
add_action
( 'publish_post
','duplicate_titles_wallfa_bc
' ) ;
function
duplicate_titles_wallfa_bc
( $post
){
global
$wpdb
;
$title
= $_POST
['post_title
'] ;
$post_id
= $post
;
$wtitles
= "SELECT
post_title
FROM
$wpdb
->posts
WHERE
post_status
= 'publish
' AND
post_type
= 'post
'
AND
post_title
= '{$title
}' AND
ID
!= {$post_id
} " ;
$wresults
= $wpdb
->get_results
( $wtitles
) ;
if
( $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
) ;
exit
;
}
}
文章提交更新后的提示
禁用自动保存
add_action
( 'wp_print_scripts
', 'disable_autosave
' ) ;
function
disable_autosave
(){
wp_deregister_script
( 'autosave
' ) ;
}
转载请注明:汇站网 » WordPress
怎么防止发表重复标题的文章?