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.
Finding the 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 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.
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 send an email to the user submitting the form when the post is published? Take a look at our tutorial on How to Send Email Notification on Post Submission Publish.
Related
Action Reference: wpforms_post_submissions_process