php判断标题重复,WordPress建站教程,纯代码实现wordpress防止发布文章出现标题重复,自动检测重复标题文章....

/**

* 发表文章时禁止与已存在的标题相重复

* Modify from Plugin: Duplicate Title Validate

* Description: this plugin help , not allow publish Duplicate Title .

* Author: hasan movahed

* Reviser: INLOJV

* Version: 1.0

*/

// 发表文章页面,前端抓取标题并使用AJAX发送请求

add_action( 'admin_print_footer_scripts', 'duplicate_titles_enqueue_scripts', 100 );

function duplicate_titles_enqueue_scripts() {

?>

jQuery(function($){

function checkTitleAjax(title, id,post_type) {

var data = {

action: 'title_checks',

post_title: title,

post_type: post_type,

post_id: id

};

$.post(ajaxurl, data, function(response) {

$('#message').remove();

$('#poststuff').prepend('

'+response+'

');

});

};

$('#title').change(function() {

var title = $('#title').val();

var id = $('#post_ID').val();

var post_type = $('#post_type').val();

checkTitleAjax(title, id,post_type);

});

});

}

// 接收前端ajax参数

add_action('wp_ajax_title_checks', 'duplicate_title_checks_callback');

function duplicate_title_checks_callback(){

global $wpdb;

$title = $_POST['post_title'];

$post_id = $_POST['post_id'];

$titles = "SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'

AND post_title = '{$title}' AND ID != {$post_id} ";

$results = $wpdb->get_results($titles);

if($results) {

echo "". _( '此标题已存在,请换一个标题!' , '' ) ." ";

} else {

echo ''._('恭喜,此标题未与其他文章标题重复!' , '').'';

}

die();

}

// 检测后端标题并且避免同名文章更新草稿

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( 'admin_notices', 'not_published_error_notice' );

function not_published_error_notice() {

if(isset($_GET['wallfaerror']) == 1 ){

?>

}

}

// 禁用自动保存

add_action( 'wp_print_scripts', 'disable_autosave' ) ;

function disable_autosave(){

wp_deregister_script( 'autosave' ) ;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值