0

I am using the User Submitted Posts plugin which I love. The only problem with this plugin is that it does not check for duplicate post titles and outputs a post with the same title, even if it already exists.

So I want to know: how can I check if someone clicks on the submit button, if a post with the same title exists and give them a 404 error? Is this even possible?

PS: it would be even better that if the post with the same title exists, the submitted would be a comment beneath that post, but I guess that's utopia.

I already tried

    if (is_null(get_page_by_title( $content['title'], OBJECT, 'post'))) {

// Do your stuff in this case (no post with the same title)

}

else {

// Do not create post

}

and

jQuery(document).ready(function($){
var titleArray = $('.row-title');
for (i=0;i<titleArray.length;i++) {
        var thisTitle = $(titleArray[i]).text();
        for (j=0;j<titleArray.length;j++) {
            if (thisTitle == $(titleArray[j]).text() && i < j ){
                $('input',$(titleArray[j]).parent().parent().parent()).attr('checked', true);
            }
        }
    }
});

But it did not do what I wanted.

4
  • 1
    Please explain what you have tried already.
    – fuxia
    Commented Jun 15, 2013 at 15:37
  • Do you want to do that without changing the plugin files or you can deal with manual plugin updates? Avoiding to change the plugin files would make things a bit more tricky, but it's still not impossible. Commented Jun 15, 2013 at 16:07
  • Well, any way would to actually Commented Jun 15, 2013 at 16:39
  • Please post the plugin code that inserts the posts.
    – s_ha_dum
    Commented Jun 16, 2013 at 13:35

2 Answers 2

1

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.

1
  • 1
    Haha damn, you are truly wonderful. Thank you very very much Nikola! It works like a charm! PS: yes, it is very frustrating that I can't see the post if I submit it, but I will check on that from here. Thanks again! Commented Jun 16, 2013 at 21:30
1

I am using this solution to check duplicate post title.

$page_check = get_page_by_title($content['title'],OBJECT,'post');
 if(!$page_check->ID){
   //do something when post not exits
  }else{
    // give a error or something
   }
3
  • Hi, thank you Vikas. Where do I have to put it? In the plugin files or functions.php? Both did not work... Commented Jun 16, 2013 at 9:09
  • you should add this in plugin where you are submitting post and make sure you have post field title in the form. Commented Jun 17, 2013 at 9:57
  • Thanks for your help and time Vikas, I gave you a upvote for your efforts. Commented Jun 17, 2013 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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