PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Assign default featured image if none input

  1. Hi there,

    I would really like to know if there's any way that I can assign a default featured image with created posts via my form if the image upload field is left blank.

    Any help would be greatly appreciated as I'm stumped!

    Many thanks,

    Cam

    Posted 7 years ago on Tuesday July 31, 2012 | Permalink
  2. Please see if this helps:
    http://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/

    <?php if ( has_post_thumbnail() ) {
    the_post_thumbnail();
    } else { ?>
    <img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />
    <?php } ?>

    Explanation: The code checks if the post has a specified thumbnail, and if there is, then it will display the post thumbnail. If there is no post thumbnail specified, then it will display ‘default-image.jpg’ image from your theme’s images folder.

    Posted 7 years ago on Tuesday July 31, 2012 | Permalink
  3. Thanks Chris,

    I did stumble on that and a few similar articles before posting on the forum here - but what I'd really like to do is assign a default featured image when the post is created via the form.

    Basically I'd like to set a default URL to an image on the server to use as the featured image of the created post if the user doesn't select to upload their own image.

    Thanks again for your help :)

    Posted 7 years ago on Tuesday July 31, 2012 | Permalink
  4. We're accomplishing the same thing, but you would like to do it differently. The code I posted will supply a default image for a post when there is no featured image, at the time the post is displayed. You want to store the default image with the post if the user does not submit an image?

    If that's the case, you will have to modify the post after the form is submitted, using the gform_after_submission hook: http://www.gravityhelp.com/documentation/page/Gform_after_submission

    Check the see if the visitor submitted an image to you, and if not, add the code to set a featured image for the post.

    Here is some old code we used to use (prior to 1.6, when the featured image feature was added): http://pastie.org/2270372

    You're going to want to assign your default image on line 14 I believe.

    Posted 7 years ago on Tuesday July 31, 2012 | Permalink
  5. That looks extremely helpful thank you so much! I will try it out now and let you know how I get on...

    Posted 7 years ago on Wednesday August 1, 2012 | Permalink
  6. OK, please do let us know how it goes.

    Posted 7 years ago on Wednesday August 1, 2012 | Permalink
  7. Where should I put the php code then? In the form_display.php file? If so where in that file?

    Sorry for being dumb...

    Posted 7 years ago on Wednesday August 1, 2012 | Permalink
  8. The default image is:
    http://www.clikaclinic.com/images/default-image.jpg
    It's ID is 1339.

    The form ID is 1.
    I'm not sure where to include this information in the code you pointed me to and also like I say not sure in which file to put the code...

    Sorry! And thanks again for being so helpful :)

    Posted 7 years ago on Wednesday August 1, 2012 | Permalink
  9. NEVER modify any of the plugin files.

    You add this code to your theme's functions.php as detailed here:
    http://www.gravityhelp.com/documentation/page/Where_Do_I_Put_This_Code%3F#PHP

    Posted 7 years ago on Wednesday August 1, 2012 | Permalink
  10. Of course, sorry... being stupid! Have I got this right then?

    /*********** Set default featured image for reviews submitted via form ************/
    // after form 1 is submitted call function
    add_filter("gform_after_submission_1", "gform_set_post_thumbnail");
    function gform_set_post_thumbnail($entry){
    // get post ID of the created post
    $post_id = $entry["post_id"];
    // get the last image added to the post
    $attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC'));
    if(sizeof($attachments) == 0)
    	return; //no images attached to the post
    // set image as the post thumbnail
    set_post_thumbnail($post_id, 'http://www.clikaclinic.com/images/default-image');
    }
    Posted 7 years ago on Wednesday August 1, 2012 | Permalink
  11. Hi Chris, I don't suppose you might be able to have a look at this today could you? Have I got the right idea with the code above?

    Many many thanks again for your help on this...

    Kind regards,

    Cam

    Posted 7 years ago on Friday August 3, 2012 | Permalink
  12. Nailed it! Code was (where 1339 is the ID of the default image in my media library):

    /*********** Set default featured image for reviews submitted via form ************/
    
    // after form 1 is submitted call function
    add_filter("gform_after_submission_1", "addreview_set_post_thumbnail");
    function addreview_set_post_thumbnail($entry){
    	// get post ID of the created post
    	$post_id = $entry["post_id"];
    	// get the last image added to the post
    	$attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC'));
    	//no images attached to the post
    	if(sizeof($attachments) == 0){
    		// set image as the post thumbnail
    		set_post_thumbnail($post_id, 1339);
    	}
    }
    Posted 7 years ago on Friday August 3, 2012 | Permalink
  13. Very good. Glad you were able to sort that out. Thank you for the update. Sorry I could not get back to it until today.

    Posted 7 years ago on Friday August 3, 2012 | Permalink

This topic has been resolved and has been closed to new replies.