I'm using wordpress. Im working on Don't submit post if post title exists with the same TAG how can i achieve that ?
The code Below is only checking the post title and its already working if it has the same post title it will echo This post is already posted would u like to post a another one How can i achieve checking the post title with the post tag ? if the post title is existed and its using the post tag it will echo this again This post is already posted would u like to post a another one
<?php
//jQuery to send AJAX request
function duplicate_titles_enqueue_scripts( $hook ) {
if( !in_array( $hook, array( 'post.php', 'post-new.php' , 'edit.php'))) return;
wp_enqueue_script('duptitles',
wp_enqueue_script('duptitles',plugins_url().'/duplicate-title-validate/js/duptitles.js',
array( 'jquery' )), array( 'jquery' ) );
}
add_action( 'admin_enqueue_scripts', 'duplicate_titles_enqueue_scripts', 2000 );
add_action('wp_ajax_title_checks', 'duplicate_title_checks_callback');
/// callback ajax
function duplicate_title_checks_callback() {
function title_checks() {
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) {
return "<span style='color:red'>". _e( 'Duplicate title detected, please change the title.' , 'dublicate-title-validate' ) ." </span>";
} else {
return '<span style="color:green">'._e('This title is unique.' , 'dublicate-title-validate').'</span>';
}
}
echo title_checks();
die();
}
// this chek backend title and if Duplicate update status draft .
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( 'publish_post',
'duplicate_titles_wallfa_bc' ) ;
/// handel error for back end
function not_published_error_notice() {
if(isset($_GET['wallfaerror']) == 1 ){
?>
<div class="updated">
<p style='color:red' ><?php _e(' This post is already posted would u like to post a another one. ' , 'dublicate-title-validate') ?></p>
</div>
<?php
}
}
add_action( 'admin_notices', 'not_published_error_notice' );
function duplicate_titles_wallfa_action_init()
{
// Localization
load_plugin_textdomain('dublicate-title-validate',false,dirname(plugin_basename(__FILE__)).'/langs/');
}
// Add actions
add_action('init', 'duplicate_titles_wallfa_action_init');
function disable_autosave()
{
wp_deregister_script( 'autosave' ) ;
}
add_action( 'wp_print_scripts', 'disable_autosave' ) ;
?>
AJAX
jQuery(document).ready(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);
});
});
This is the Explanation Example
Post Name - Post Tag - Result
post 1 - post1 - allowed
post 2 - post1 - allowed
post 1 - post1 - This post is already posted would u like to post a another one. (because it has the same value of the first post )