Support » Plugin: Duplicate Title Checker » Suggestions

  • fuchsws

    (@fuchsws)


    great plugin, exactly what I needed. I have a few suggestions to make it even better:

    _ include all post_status, not only published posts (or maybe exclude only revisions and auto-saves)

    _ use “post_title LIKE ‘%{$title}%'”, which might of course return more results, but might find similar posts titles too

    _ output found post titles, so the editor can decide if it’s duplicated or not

    function duplicate_title_check_callback() {
    
    	function title_check() {
    
    		$title = $_POST['post_title'];
    		$post_id = $_POST['post_id'];
    
    		global $wpdb;
    
    		$sim_titles = "SELECT post_title,ID FROM $wpdb->posts WHERE post_type = 'post' AND post_title LIKE '%{$title}%' AND ID != {$post_id} ";
    
    		$sim_results = $wpdb->get_results($sim_titles);
    
    		if($sim_results) {
    			$titles = '<ul>';
    			foreach ( $sim_results as $the_title )
    			{
    				$titles .= '<li><a href="'. get_edit_post_link($the_title->ID) .'" target="_blank">'.$the_title->post_title.'</a></li>';
    			}
    			$titles .= '</ul>';
    
    			return '<div style="color:red">Post title seems to exist already: '.$titles.'</div>';
    		} else {
    			return '<div style="color:green">Post title seems to be unique</div>';
    		}
    	}
    	echo title_check();
    	die();
    }

    https://wordpress.org/plugins/duplicate-title-checker/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author ketanajani

    (@ketanajani)

    Hello Fuchsws,

    Thank you very much for using this plugin and for your positive views too. I’ve created this plugin for general purpose but your suggestions can be great additions to this plugin. I will implement the features you suggested in next update. Thank you very much for your comments to make this plugin better and much more useful.

    I edited the plugin based on fuchsws additions so it could be used with custom post types and ‘post title is unique’ message would not appear – it seems to be overinformative.

    duplicate-title-checker.php:

    <?php
    /*
    	Plugin Name: Duplicate Title Checker
    	Plugin URI: http://wordpress.org/extend/plugins/duplicate-title-checker/
    	Description: This plugin provides alert message for duplicate post title and unique post title when adding new post.
    	Author: Ketan Ajani, additions by fuchsws and certainlyakey (see http://wordpress.org/support/topic/suggestions-35?replies=2)
    	Version: 1.0
    	Author URI: http://www.webconfines.com
    */
    
    //////////////////////////this is duplicate title check///////////////////////////////////
    
    	//jQuery to send AJAX request - only available on the post editing page
    	function duplicate_titles_enqueue_scripts( $hook ) {
    
    		if( !in_array( $hook, array( 'post.php', 'post-new.php' ) ) )
    			return;
    
    		wp_enqueue_script( 'duptitles',wp_enqueue_script( 'duptitles',plugins_url().'/duplicate-title-checker/js/duptitles.js',array( 'jquery' )), array( 'jquery' )  );
    	}
    	add_action( 'admin_enqueue_scripts', 'duplicate_titles_enqueue_scripts', 2000 );
    
    	add_action('wp_ajax_title_check', 'duplicate_title_check_callback');
    
    function duplicate_title_check_callback() {
    
    	function title_check() {
    
    		$title = $_POST['post_title'];
    		$post_id = $_POST['post_id'];
    		$post_type = $_POST['post_type'];
    
    		global $wpdb;
    
    		$sim_titles = "SELECT post_title,ID FROM $wpdb->posts WHERE post_type = '{$post_type}' AND post_title LIKE '%{$title}%' AND ID != {$post_id} ";
    
    		$sim_results = $wpdb->get_results($sim_titles);
    
    		if($sim_results) {
    			$titles = '<ul>';
    			foreach ( $sim_results as $the_title )
    			{
    				$titles .= '<li><a href="'. get_edit_post_link($the_title->ID) .'" target="_blank">'.$the_title->post_title.'</a></li>';
    			}
    			$titles .= '</ul>';
    
    			return 'Post title seems to exist already: '.$titles;
    		}
    		  else {
    		 	return 'Post title seems to be unique';
    		 }
    	}
    	echo title_check();
    	die();
    }
    
    	function disable_autosave() {
    		wp_deregister_script('autosave');
    	}
    	add_action( 'wp_print_scripts', 'disable_autosave' );
    
    ?>

    duptitles.js:

    jQuery(document).ready(function($){
        // Post function
        function checkTitle(title, id,post_type) {
            var data = {
                action: 'title_check',
                post_title: title,
                post_type: post_type,
                post_id: id
            };
    
            //var ajaxurl = 'wp-admin/admin-ajax.php';
            $.post(ajaxurl, data, function(response) {
                $('#message').remove();
                if (response != 'Post title seems to be unique') {
                    $('#poststuff').prepend('<div id=\"message\" class=\"updated fade\" style=\"color:red\"><p>'+response+'</p></div>');
                }
            });
        };
    
        // Add button to "Check Titles" below title field in post editor
        //$('#edit-slug-box').append('<span id="check-title-btn"><a class="button" href="#">Check Title</a></span>');
    
        // Click function to initiate post function
        $('#title').change(function() {
            var title = $('#title').val();
            var id = $('#post_ID').val();
            var post_type = $('#post_type').val();
            checkTitle(title, id,post_type);
        });
    
    });

    for an added layer of security we should be using $wpdb->prepare

    The following is an example:

    $sim_query = "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'venue' AND post_title LIKE '%%%s%%' AND ID != '%d'";
    $sim_results = $wpdb->get_results( $wpdb->prepare( $sim_query, $title, $post_id ) );

    I too would like more permissive results but given the above, Im not seeing any variation with longer strings (ie. most every title).

    This plugin is Nice is there a way to make it available on the front end

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Suggestions’ is closed to new replies.