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?
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.
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() ) {
}
}
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
}
});
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?
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/
My issue is resolved now. Thank you!