Skip Navigation

[Resolved] Adding default image via a Post form

This support ticket is created 6 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by alanB-6 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1120681

Tell us what you are trying to do?
I have created a Post from for a custom post and would like to apply a default featured image rather than relying on my users to select an image when they enter a post.

Is there any documentation that you are following?
none although there is a previous post https://toolset.com/forums/topic/cred-form-upload-default-feature-image-and-save-it-with-post-if-none-uploaded/ which seems to cover this but I am not sure where in the post form builder I would need to add the suggested code.

Is there a similar example that we can see?
Not sure

What is the link to your site?
newsite.highdownchurch.org.uk

#1120777

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Alan

The code in the linked thread should do the job: https://toolset.com/forums/topic/cred-form-upload-default-feature-image-and-save-it-with-post-if-none-uploaded/#post-320291

You'll need to edit the id of the form, and the id of the image to use for the default (from the media library).

This code is PHP, to run on your server, it isn't added to the form itself.

In Types 3.1 we added a new feature to make it easier to add custom code, which you can read about here: https://toolset.com/documentation/adding-custom-code/

If you get stuck, let me know.

#1120819
Capture1.PNG

Thanks for coming back to me...

Its not working for me if you could take a look it would be great.

My code looks like this...

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==356)
    {
        if (empty($_POST['_featured_image']))
        {
            // add it to saved post meta
     add_post_meta( $post_id, "_thumbnail_id",'367' );
        }
    }
}

The image ID is 367 and the ID for the post form is 356 I have attached screenshot of custom code

#1121571

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Alan

I just tried your code and it appeared to work, but on closer inspection I realise it won't work properly because image fields are uploaded via ajax before the form itself is submitted.

I reworked your code to check whether there is already a thumbnail attached, and if not, to then add it.

<?php
add_action('cred_save_data', 'tssupp_default_thumbnail', 10, 2);
function tssupp_default_thumbnail($post_id, $form_data) {

	if ($form_data['id'] == 356) {

		$current_thumb = get_post_meta($post_id, '_thumbnail_id', true);

		if (empty($current_thumb)) {

			add_post_meta($post_id, "_thumbnail_id", '367');
		}
	}
}
#1121594
Capture1.PNG

Still same problem I'm afraid post gets added but still not adding image as posts featured image

If I manually add image via post form it does get added as expected to the post but if no image manually added the default does not get added to the post.

I have been unable to add another image of the code snippet (window does not open).

But settings are
Run mode set to Run Always
Run context WordPress admin and Ajax calls

not sure if these are correct

On editing post have been able to add image

#1121610

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You have "Front-end" unchecked, even though this is a front-end form submission.

I tried the same and found the same. Check that and it should work.

#1121740

My issue is resolved now. Thank you!