Skip Navigation

[Resolved] I cannot get featured image in CRED form to save against post

This support ticket is created 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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+01:00)

This topic contains 26 replies, has 2 voices.

Last updated by Nigel 7 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#421723

Nigel

Thanks for persevering... sorry this had to go on to the backburner for a while.

I had another look at everything and still can't figure it out...

The settings on the Post type are set to allow the upload of a 'featured image' to the post (a.k.a. 'thumbnail').

I regenerated the form.

The form works in that the featured image is loaded and saved to the media library.

The featured image just doesn't attach to the Post it is uploaded with.... It used to work... but not any more.

needing a solution... would love to get this working... otherwise its find a work around... I switched from an image field in the CPT to featured image to enable me to auto tweet with the image from the Announcements RSS feed. I had set this up on Zapier and it worked fine... then it stopped.

Any ideas.. if I can get the auto tweeting working from the Custom image field rather than the Featured image.. then this would be problem solved.

Cheers

Peter

#421828

Nigel
Supporter

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

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

Hi Peter

Just to let you know I'm still working on this.

Using the snapshot I took the other day I haven't been able to get the form to *not* work.

I went into your staging area site and repeated the steps and there the image didn't attach. It looks as if you made a few changes in the interim and so I am currently taking a new snapshot and I do some further testing. I get back to you as soon as I can.

#422346

Nigel
Supporter

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

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

Hi Peter

I took a new snapshot to include any changes you had made.

I had the same experience as before, where the column entries for Type and Post Type for the New Company Announcement Form were empty, so I had to re-save the form having updated the relevant dropdowns.

I then tried making test announcements from a different company to last time using the featured image and the image attached to the post as expected, i.e. I couldn't reproduce the problem.

I'm going to consult with my colleagues about what testing or logging I might be able to set up on your staging server to identify the issue.

#422862

Nigel
Supporter

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

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

Hi Peter

It have been asked to see if you can review your Apache server logs (hidden link) to see if they reveal any SQL expression errors when you submit the CRED form.

They might reveal something which doesn't show up in the WordPress logs.

#422910

Nigel
Supporter

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

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

Hello again Peter

Please go ahead with the previous step to inspect your server logs, if possible.

But in the meantime I thought you might want to add a snippet of code as a temporary fix which should make sure that the image gets attached as the featured image.

There is a new version of CRED currently beginning testing and, with your somewhat esoteric issue, it could be that it resolves itself with the new release.

Until then, you could add the following to your child theme functions.php file, which should attach the featured image if it hasn't already been.

Note the form id is hard-coded into the snippet and may need editing if you replace the form.

/**
 * Custom code to force add thumbnail meta
 * to add announcement CRED form post
 */
function force_thumbnail( $post_id, $form_data ) {

	if ( $form_data['id'] == 1564 && !empty( $_POST['attachid__featured_image'] ) ) {

		add_post_meta( $post_id, "_thumbnail_id", $_POST['attachid__featured_image'], true );

	}
}
add_action( 'cred_save_data', 'force_thumbnail', 10, 2);
#426938

Nigel

Sorry I had missed this last message.. the earlier one stumped me as I do not have easy access to those logs due to my hosting set up...

I have just tried the code you provided but got the following error.. that I can't figure out.

Parse error: syntax error, unexpected '&', expecting ']' in /nas/content/staging/techboard/wp-content/themes/sparkling-child/functions.php on line 269

Could you please suggest how the code needs to be changed...

By the way I have recently updated the CRED plugin to 1.7.1 but that did not fix this problem.... I have however gone through and cleaned out a whole bundle of plugins but that also did not make any difference.

Please help.

Peter

#426997

Nigel
Supporter

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

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

Hi Peter

I just re-tested my copy of the site with that code snippet added to my functions.php and it works as expected, so I'm wondering if something went wrong copying and pasting it?

I checked your functions.php via FTP but, of course, you have deleted the code, so I have added it myself. I can't test because the user credentials you gave me before no longer seem to work.

Could you please test again and let me know how you get on?

#427006
Screen Shot 2016-08-23 at 4.09.46 PM.png

Nigel

Yes I must have made an error pasting it.

I have tested and it still saves to Media but does not attach to the post... very frustrating...

see screenshot below.

Peter

#427027

Nigel
Supporter

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

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

Hi Peter

Sorry about this, it is very frustrating.

I have added a slew of error_log statements to the code in your functions.php to see why it might not be working.

I can't run the test though because I can't log in to your site. I'll mark your next reply as private in case you want to give me fresh credentials to do so, or if you want to go ahead an run a test yourself and then copy and paste the results from the debug.log, or let me know when you have and I can check via FTP.

#427122

Nigel
Supporter

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

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

Hi Peter

The workaround works, at last.

I had one of those frustrating afternoons starting at the screen wondering what was missing when it was right in front of me. You'll notice a lot of test posts.

So, I updated my code added to your functions.php file, which now looks like this:

/**
 * Custom code to force add thumbnail meta
 * to add announcement CRED form post
 */
function force_thumbnail( $post_id, $form_data ) {

    if ( $form_data['id'] == 1564 && !empty( $_POST['_featured_image'] ) ) {

        $args = array(
            'numberposts'   => 1,
            'post_type'     => 'attachment',
            'post_parent'   => $post_id
        );
        $thumb = get_posts( $args );

        $meta_update = add_post_meta( $post_id, "_thumbnail_id", $thumb[0]->ID, true );

    }
}
add_action( 'cred_submit_complete', 'force_thumbnail', 10, 2 );

I also noticed some PHP warning notices coming from your own custom code, specifically for your my_validation function. This needs to return $field_data, but you were returning something else (which didn't necessarily exist and was throwing the warnings). You should review that block of code, you will see I added a comment.

#427128

Nigel

I saw you were doing lots of tests. Thanks so much for getting it working.

I will have to have a look at that validation issue...

Thanks so much for your persistence

Peter

#432971

Nigel
Supporter

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

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

Hi Peter

Just to let you know, after a number of other users reported similar issues the problem was identified and a hotfix has been issued, as described here: https://toolset.com/errata/featured-image-uploaded-not-attached-posts-created-using-cred-form/

It will be properly fixed in the next CRED release. I'm not sure if you want to swap a workaround for a hotfix, but it's there if you need it.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.