How to Set a Default Featured Image for Post Submissions

Introduction

Would you like to set a default featured image for post submissions? By setting your own featured image for your submissions, you can easily control what image is set when you approve your post submissions. Using a small PHP snippet, this tutorial will walk you through how to achieve this.

Please note, using this snippet means that all submissions with this form ID will have the exact same featured image set for each post.

Creating the form

First, we’re going to create a new form using the Post Submission addon.

If you need any help in creating this type of form, please review this documentation.

create your post submission form

Find thing image ID number

In order to set the default featured image, you’ll need to know the image ID number from your WordPress Media Library.

Just log into your WordPress admin and click on the Media » Library, once you’ve selected the the image to use as your default featured image, click Edit.

In the browser’s address bar, you’ll see the URL. Find the number referenced in the URL and this is your image ID number. For our tutorial, our number is 1028.

find the image id number so that you can set a default featured image for every post submission

Adding the snippet

Now it’s time to add the snippet to our site. If you need help in where and how to add snippets to your site, please check out this tutorial.

/**
 * Assign default featured image
 *
 * @link https://wpforms.com/developers/how-to-set-a-default-featured-image-for-post-submissions/
 */
 
function wpf_dev_post_submissions_process( $post_id, $fields, $form_data ) {
       
    // If form ID is 463, run code below
    if ( 463 !== absint( $form_data[ 'id' ] ) ) {
        return;
    }
     
    // Assign image as the default featured image
    // Replace 1028 with the image attachment id
    $image_id = absint(1028);

    set_post_thumbnail($post_id,$image_id);
 
    }
 
add_action( 'wpforms_post_submissions_process', 'wpf_dev_post_submissions_process', 10, 3 );

This snippet will only run on the form ID 463. You’ll need to update this ID to match your own form ID.

For any assistance in finding your form ID number, please visit this tutorial.

Now for each new post submission entry received, this snippet will set a default featured image. Would you like to use the Post Submissions addon for things other than blog posts? Check out our article on How to Use the Post Submissions Addon to Post to Other Post Types.

Action Reference: wpforms_post_submissions_process