Don’t submit post if post title exists

Here’s a sample code to get you started – basically it intercepts the creation of a new post by the user-submitted posts plugin(well at least it tries to, but in general there shouldn’t be any other posts created during that request). If a post with that name already exists, we add the content as a comment to that post and redirect to the comment’s URL, otherwise everything else is left up to the plugin itself.

// Hook to the "wp_insert_post_empty_content" filter, since that is the only place
// we can intercept the creation of a new post and not let it be created
add_filter( 'wp_insert_post_empty_content', 'intercept_user_submitted_post', 10, 2 );
function intercept_user_submitted_post( $maybe_empty, $post_data ) {
    if ( isset( $_POST['user-submitted-post'] ) && ! empty( $_POST['user-submitted-post'] ) ) {
        $target_post = get_page_by_title( $post_data['post_title'], OBJECT, 'post' );

        // We found an existing post!
        if ( $target_post && $target_post->ID ) {
            global $usp_options;

            if ( stripslashes( $_POST['user-submitted-name'] ) && ! empty( $_POST['user-submitted-name'] ) ) {
                $author_submit = stripslashes( $_POST['user-submitted-name'] );
                $author_info = get_user_by( 'login', $author_submit );

                if ( $author_info ) {
                    $authorID = $author_info->ID;
                    $authorName = $author_submit;
                    $authorEmail = $author_info->user_email;
                } else {
                    $authorID = $usp_options['author'];
                    $authorName = $author_submit;
                    $user_data = get_userdata( intval( $authorID ) );
                    $authorEmail="";
                }
            } else {
                $authorID = $usp_options['author'];
                $authorName = get_the_author_meta( 'display_name', $authorID );
                $user_data = get_userdata( intval( $authorID ) );
                $authorEmail = $user_data->user_email;
            }
            $authorUrl = stripslashes( $_POST['user-submitted-url'] );

            $time = current_time('mysql');
            $data = array(
                'comment_post_ID' => $target_post->ID,
                'comment_author' => $authorName,
                'comment_author_email' => $authorEmail,
                'comment_author_url' => $authorUrl,
                'comment_content' => $post_data['post_content'],
                'comment_type' => '',
                'comment_parent' => 0,
                'user_id' => $authorID,
                'comment_author_IP' => preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] ),
                'comment_agent' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '',
                'comment_date' => $time,
                'comment_approved' => 1,
            );

            $comment_id = wp_insert_comment( $data );

            // Redirect to the comment URL
            wp_redirect( get_comment_link( $comment_id ) );
            exit;
        }
    }

    return $maybe_empty;
}

Place that code in your theme’s functions.php and that should be all.


PS: There’s more room for improvement in that code, like displaying a message when the user is redirected, or adding the photos to the comment as well, but I’ll leave that up to you. I think the above code is a good place to start from.