Skip Navigation

[Resolved] Pre-populate custom image

This support ticket is created 6 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1103277

We use a repeatable custom image field and would like it to be pre-populated (with one image) as an example when displaying it in a form. Is this possible and what would be the best way to achieve this?

#1103764

Hi, you can use the "value" attribute of the field shortcode to supply a default image URL, and that image will be automatically populated in the Form when the page loads. Add a default image to your Media Library, then copy the full image URL. Insert the image URL into the value attribute of the field shortcode like this:

[cred_field field='your-field-slug' value='<em><u>hidden link</u></em>' urlparam='' output='bootstrap']
#1103796

Thanks for the reply. You're correct but I realise my problem is I need to add the default image for a custom post already created (in Gravity Forms). So it's not a new post but it's when editing an existing post where no images have been added yet.

Maybe it would be easier to do this in php and add a default image (from the media library) if no images are added yet?
But I don't know how the repeating image fields works (or how I access them from PHP) and how to link an image to it. Any tips?

#1104637

There's not an easy way to set up a conditional in an edit post Form that will populate a repeating image field with a default value only if no other value is already set. Is there any reason not to automatically set this custom field value when the post is created by Gravity Forms? If not, you can use the WordPress add_post_meta method to set the value of a custom image field with any URL. Use a save_post hook or use any hook offered by Gravity Forms to add your image. Check first to be sure you're not overwriting anything that already exists:

...
$rptImg = get_post_meta( 12345, 'wpcf-repeating-image', true);
if( ! $rptImg ) {
  add_post_meta( 12345, 'wpcf-repeating-image', '<em><u>hidden link</u></em>');
}
...

Change 12345 to be the id of the new post, or a variable representing the new ID. Change 'wpcf-repeating-image' to match the slug of your custom image field. Types fields use a prefix of 'wpcf-' in the database, so if your image field slug in wp-admin is 'my-img' then you should change this to 'wpcf-my-img'. Change the URL to match an image in your Media Library.