Skip Navigation

[Resolved] Toolset FORMS: Use featured image from other server – how to prevent uploading?

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

Problem:
The user would like to use images from URLs instead of uploading them.

Solution:
This is not a featured offered by WordPress, Toolset does not offer it either. But, we can still use a 3rd party plugin, a Toolset URL field, and custom code to make it work.
Check this reply https://toolset.com/forums/topic/toolset-forms-use-featured-image-from-other-server-how-to-prevent-uploading/#post-1914255

Relevant Documentation:

This support ticket is created 3 years, 11 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: Africa/Casablanca (GMT+01:00)

This topic contains 10 replies, has 2 voices.

Last updated by a.R 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1907647

a.R

Hi 🙂

Is it possible simply to input an address for the featured image in FORMS - WITHOUT uploading it to media library?

Thank you 🙂

#1908043

Hello and thank you for contacting the Toolset support.

No, that's not possible. The featured image in Forms is an image field, it expects to upload the image. WordPress does not allow to use URLs for the featured image, it expects an uploaded image. But there are plugins that allow that, such as "Featured Image by URL" https://wordpress.org/plugins/featured-image-by-url/

The easiest way I can imagine is to use a custom URL field for the image instead of the featured image. Then, you can adapt your designs, using a content template, to display an image from this field instead of the featured image. Does it make sense?
Check this article hidden link

I hope this helps. Let me know if you have any questions.

#1908523

a.R

Yeah what you say makes SOME sense, thank you.

But it's REALLY a PAIN not to have the featured image set, as SO MANY things depend on it. ..

...only because somebody just ignores the possibility that it MAY be on another server and thus just doesn't make that LITTLE effort to JUST let the user CHOOSE witch kind of input field he wants to use.
So there should be quite a simple patch to change that behaviour?
And I really hope this little thing will soon be implemented in toolset.

So could you please give me a patch?

Thank you, kind regards.

Achim

#1908901

Hello Achim, I can't really agree with you. This feature is not available in WordPress, let alone in Toolset. I would say that the whole community ignores it, except for the author and the user(6000 installations) of the plugin that I shared before.
This is not something that can be handled with a patch, as it needs to be patched in WordPress.

What I would suggest is to use an URL field to store the URL of the distant image, then, you can use WordPress hooks to serve this URL for the featured image instead of the uploaded one(if it exists). You can use for example the wp_get_attachment_image_src hook:
- https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_src/
But, as you said, too many things depend on it, and maybe this hook is not enough.

So, please try this hook, and if you need further assistance, let us know with more details on where it is not working as you would expect and we'll do our best to help you.

#1912887

a.R

Hi, thank you.
This guy seems quite serious:
https://wordpress.org/plugins/featured-image-from-url/
Could there be a solution based on that?

Thank you, kind regards,

Achim

#1914255

Hello Achim,

According to the description of the plugin, there should be a solution using a Toolset URL field, the plugin's function fifu_dev_set_image (check this screenshot hidden link), and some custom code.

Let's create a URL field called my-featured-image, add it to your form then use the cred_save_data hook to save this URL as the featured image URL using the plugin's function.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Something like the following should work:

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']==12)
    {
        // Get the image URL
        $image_url = get_post_meta( $post_id, 'wpcf-my-featured-image', true);
        // Set the image URL using the plugin's function
        fifu_dev_set_image( $post_id, $image_url );
    }
}

Note that we need to add the prefix 'wpcf-' to the Toolset field's slug if we are using WordPress functions.
https://developer.wordpress.org/reference/functions/get_post_meta/

#1914723

a.R

Hi Jamal 🙂

This sounds good, but somehow I just can´t get that to work now.
Will look into this later today or tomorrow.
Changed it to

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']==6346) || ($form_data['id']==6924) || ($form_data['id']==66005) || ($form_data['id']==7000))
    {
        // Get the image URL
        $image_url = get_post_meta( $post_id, 'wpcf-fifu-featured-image-acrh', true);
        // Set the image URL using the plugin's function
        fifu_dev_set_image( $post_id, $image_url );
    }
}

Shouldn´t be a problem (with adaptions in the forms), right?

THANK YOU so far, kind regards,

Achim

#1914821

Awesome Achim,

I'll set this ticket as waiting for your response until you be able to work on it. Let me then know if you still need assistance.

All the best.

#1915867

a.R

Hi 🙂

It just won´t work for me.

Can´t you just bild it and test it?

Cheers, Achim

#1916385

I run a test on a clean site and I was able to use generic field to pass an URL of the featured image, then with custom code, we save the Image's URL using the plugin's function:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  $forms = array(16);
  
  if ( in_array( $form_data['id'], $forms ) ) { 
    if ( isset( $_POST['_wp_attached_file'] ) ) {
      $image_url = $_POST['_wp_attached_file'];
      fifu_dev_set_image( $post_id, $image_url );
    }
  }
}

Check this screencast

You can log in to this test with this URL hidden link
Check the form here hidden link
Check the page where the form is used here hidden link
The custom code is added to a Toolset custom code snippet here hidden link
Make sure that the snippet is active for frontend and AJA calls. Check this screenshot hidden link

You will need to change line 11 to enter your form or forms IDs. If you want this code to run for multiple forms, add their IDs separated by comma. For example:

  $forms = array(16, 21, 34); // This will run for forms with IDs 16, 21 and 34
#1916695

a.R

Ok Jamal, now it works.

Didn´t know I had to add a GENERIC field to our forms.
You told me to CREATE a url field, THEN add it to the form.

Thanks again, kind regards.

Achim