Preventing duplicate posts with wp_insert_post in a shortcode or block

  |   By  |  0 Comments

If you want to programmatically insert a post in the front end, for example based on user input, there is a danger that the user can resubmit the form and post it twice.

WordPress has the function post_exists to prevent that. But it is an admin function and if you try using it in the front end you will get an undeclared function error. Here’s how to prevent that. You just need to include post.php!

if ( ! function_exists( 'post_exists' ) ) {
            require_once( ABSPATH . 'wp-admin/includes/post.php' );
        }
if(post_exists( $post_title, $post_content, '', $post_type, '' ))return;
name

ABOUT THE AUTHOR - ANDY MOYLE