How to prevent articles with duplicate titles from being published?

Prevent publication of articles with the same title 

I usually post articles for external links or something,The webmasters are busy,Sometimes articles with the same title are published,At this time, you can use this string of codes to make a simple prediction.。

note:Recommend to use under traditional editor。

When you publish the article,Check for duplicate article titles

Put this function in the theme root directoryfunctions.php File <?phpSave it below

// Published article page,The front end grabs the title and uses itAJAXsend request
add_action( 'admin_print_footer_scripts', 'duplicate_titles_enqueue_scripts', 100 );
function duplicate_titles_enqueue_scripts() {
?>
<script>
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('<div id="message" class="updated below-h2 fade "><p>'+response+'</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
}



// Receiving front endajaxparameter
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 "<span style='color:red'>". _( 'This title already exists,Please change the title!' , '' ) ." </span>";
} else {
echo '<span style="color:green">'._('Congratulations,This title is not a duplicate of other article titles!' , '').'</span>';
}
die();
}

// Detect back-end titles and avoid updating drafts of articles with the same name
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 ;
}
}



/// Tips after the article is submitted and updated
add_action( 'admin_notices', 'not_published_error_notice' );
function not_published_error_notice() {
if(isset($_GET['wallfaerror']) == 1 ){
?>
<div class="updated">
<p style='color:red' ><?php _e('It looks like an article with the same title already exists,If you use the article alias as a permalink,You can modify the permalink of this article to make the title no longer repeated!' , '') ?></p>
</div>
<?php
}
}



// Disable auto save
add_action( 'wp_print_scripts', 'disable_autosave' ) ;
function disable_autosave(){
wp_deregister_script( 'autosave' ) ;
}
Related Article
Manually open WordPress site maintenance mode

In an updater or plug-in,WordPressThe front end will show that it is in maintenance mode,If you want to temporarily enable Read more

WordPress gets the subcategory of the current category

In productionWordPressWhen the theme is often met, how to be inWordPressThe category page displays the subcategories under the current category Read more

The method of forbidding the automatic saving of WordPress articles

Before, Xiao Bian talked about it《solveWordPressarticleidA simple method of discontinuity》Mentioned inWordPress自动保存article的时间间隔的问题,And today we are talking about direct prohibitionWordPressarticle自动保存的方法,The method is Read more

WordPress using CSS3 to achieve page fade animation effects

utilizeCSS3Animation properties“@keyframes ”Can achieve some dynamic effects,Specific syntax and parameters can be learned online。这篇文章主要是实践应用一下这个Animation properties,Realize page fade in effect,On foxes24edition、chrome29edition、IE10Passed Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image