Skip Navigation

[Resolved] How to default featured image field to that of the related post

This thread is resolved. Here is a description of the problem and solution.

Problem:

In the "post-new-deal" form, I have a field for the featured image of the post. I would like to default the value to the featured image of the Lodge post.

Solution:

In this case, you might consider custom codes, for example:

https://toolset.com/forums/topic/how-to-default-featured-image-field-to-that-of-the-related-post/#post-1506683

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 4 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by ericE-4 4 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1506287

I have a view for my "Lodge" post types, called "post-new-deal-form". In it, I display a form for adding a new "Deal" post:

[cred_form form="post-new-deal"]

In the "post-new-deal" form, I have a field for the featured image of the post. I would like to default the value to the featured image of the Lodge post (from the view that spawned the form).

I have tried this:

[cred_field field="_featured_image" output="bootstrap" value='[wpv-post-featured-image size="1536x1536" output="url"]']

But it doesn't work.

Can you help?

#1506683
feature-image-field.JPG

Hello,

I assume we are talking about a post form for creating new "Deal" post.

The featuread image field does not value attribute, you should able to see there isn't "Field default value" input box for feature image field, see screenshot feature-image-field.JPG, so your codes won't work as expected.

In your case, you might consider custom codes, for example:
1) You can get current post's featured image ID with shortcode:
[wpv-post-featured-image output="id"]
2) In your form content, display a hidden generice field "default-image", use above shortcode as default value, for example:
[cred_generic_field type='hidden' field='default-image']
{
"default":"[wpv-post-featured-image output="id"]"
}
[/cred_generic_field]
3) After user submit the form without any feature image selected, use action hook cred_save_data, to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
4) In above PHP function:
- Check if the new post has featured image
https://developer.wordpress.org/reference/functions/has_post_thumbnail/
If it hasn't, do below:
- get the field "default-image" value:
hidden link
- Update the new post, use "default-image" value as feature image:
https://developer.wordpress.org/reference/functions/set_post_thumbnail/

For your reference.

#1508379

OK I've tried to piece together what you instructed, but I'm far from a php expert. Can you explain how the code should go for "- get [get the field "default-image" value] and [Update the new post, use "default-image" value as feature image]?

This is what I could figure out on my own:

add_action('cred_save_data_3337', 'save_data_for_form_with_id_3337',10,2);
if ( !has_post_thumbnail() ) {

}
}

#1508451

For example, your post form ID is 3337.

You can try below PHP codes:

add_action('cred_save_data_3337', function($post_id, $form_data){
    if ( !has_post_thumbnail($post_id) && isset($_POST['default-image'])) { // If there isn't feature image submitted
		set_post_thumbnail($post_id, $_POST['default-image']); // setup the feature image
	}
});
#1509393

Unfortunately it's not working. I'm getting a 500 (Internal Server Error) when the form is submitted. The problem is something to do with the something to do with the custom code, as when it's removed, the form is submitted without error.

I copied and pasted the code you supplied verbatim.

This is how that hidden field looks:

	[cred_generic_field type='hidden' field='default-image']
      {
      "required":0,
      "default":"[wpv-post-featured-image output='id']"
      }
	[/cred_generic_field]

Ideas?

#1512267

You are right, there is a bug I mentioned above, please try to modify it as below:

add_action('cred_save_data_3337', function($post_id, $form_data){
    if ( !has_post_thumbnail($post_id) && isset($_POST['default-image'])) {
		set_post_thumbnail($post_id, $_POST['default-image']);
	}
}, 10, 2);

I have tested it in my localhost, it works fine.

More help:
https://developer.wordpress.org/reference/functions/add_action/

#1513569

My issue is resolved now. Thank you!