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
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
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.
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 :)
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.
That looks extremely helpful thank you so much! I will try it out now and let you know how I get on...
OK, please do let us know how it goes.
Where should I put the php code then? In the form_display.php file? If so where in that file?
Sorry for being dumb...
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 :)
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
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');
}
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
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);
}
}
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.