Prevent empty Post Title on form submit via front end post (wp_insert_post_)

Simply put the wp_insert_post call inside your conditional check so its only called if the post title is not empty, something like this:

if (empty($_POST['my_title'])){  
    echo 'error: please insert a title';
} else {  
    $title =  $_POST['my_title'];
    $new_post = array(
     'post_title'    => $title,
     'post_status'   => 'publish',
     'post_type'     => 'post');
    $pid = wp_insert_post($new_post);  
} 

Leave a Comment