Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am looking for some code where member can't use the same title as another post already having or used.

e.g if there is a post with title "Amazing Australia Tour" than it should not allow to use the same title to same or other user.

share|improve this question
add comment

1 Answer

Main Code

Please check the auxiliary code after this block

/*
 * Prevent Duplicated Titles
 *
 */

if( is_admin() ) // check if we are in the administrative area
{
    add_action( 'save_post', 'wpse_54258_check_for_duplicate_title', 11, 2 );
    add_action( 'admin_head-post.php', 'wpse_54258_check_for_notice' );
}

/*
 * Checks for more than one post with the same title
 *
 * Adds filter redirect_post_location if duplicate title found
 *
 */

function wpse_54258_check_for_duplicate_title( $post_id, $post )
{
    // HERE, FURTHER FILTERING CAN BE DONE, RESTRICT USERS, POST_TYPES, ETC
    if ( 
        ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        or ! current_user_can( 'edit_post', $post_id )
        or wp_is_post_revision( $post )
        // ADD OTHER FILTERS, LIKE post_type
    )
    { // Noting to do.
        return;
    }

    $termid = get_post_meta($post_id, '_is_dup', true);
    if ( '' != $termid ) 
    {
        // it's a new record
        $count_dups = 0;
        update_post_meta($post_id, '_is_dup', 'new-post-check');
    } 
    else 
    {
        $count_dups = 1;
    }

    // NO CHECKING IS BEING DONE REGARDING UPPER AND LOWERCASES, NOR FOR HTML TAGS
    global $wpdb;
    $title_exists = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = '$post->post_title' AND post_status = 'publish'") );

    if( count($title_exists) > $count_dups ) 
        add_filter('redirect_post_location','wpse_54258_add_error_query_var');
}


/*
 * Removes the previous applied filter and adds error var to the redirect
 *
 */

function wpse_54258_add_error_query_var( $loc ) 
{
    remove_filter( 'redirect_post_location','wpse_54258_add_error_query_var' );
    return add_query_arg( 'duplicated_title', 123, $loc );
}


/*
 * Error checking after saving the post
 *
 */

function wpse_54258_check_for_notice()
{
    if( isset( $_GET['duplicated_title'] ) )
        add_action( 'admin_notices', 'wpse_54258_display_error_message' );
}

/*
 * Actual error message for duplicated post titles
 *
 */
function wpse_54258_display_error_message()
{ ?>
    <div class="error fade">ERROR</div>
    <?php
    remove_action( 'admin_notices', 'wpse_54258_display_error_message' );
}

Auxiliary Code

The method used to check if a new post/page is being created or not requires a post_meta that has to be applied to all previous posts/pages.

/*
 * Update ALL PUBLISHED posts and pages with the controller post_meta required by the main code
 *
 * Important: Run Only Once 
 * -> Paste in functions.php
 * -> Remove the comment to add_action
 * -> Visit any administrative page
 * -> Delete or disable this code
 * 
 */
//add_action('admin_init','wpse_54258_run_only_once');
function wpse_54258_run_only_once()
{   
    global $wpdb;
    $allposts = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_status = 'published'" );
    foreach( $allposts as $pt )
    {
        update_post_meta( $pt->ID, '_is_dup', 'new-post-check');
    }
}

This answer was assembled using:

share|improve this answer
    
This code only checking while admin publishing but not when contributor submitting for review. I want it check at the first time when submitting the post. second thing while admin publishing the post its showing error text (only Error) but its not stopping to publish. –  pixelngrain Jun 5 '12 at 18:53
    
The is_admin only checks if we are in the Administrative pages and not in the front site. Yes, you are right, I was only testing with existing posts/pages, will take a look to catch the "new post" publishing. But for blocking the publishing, I think jQuery/Ajax would be the way to go, but I don't know much about it. Unless someone jumps in and offers an alternative solution. –  brasofilo Jun 5 '12 at 19:07
    
@pixelngrain Answer updated to catch the new post creation moment. There are some important comments in the function wpse_54258_check_for_duplicate_title on how you can fine tune it. –  brasofilo Jun 5 '12 at 19:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.