Skip Navigation

[Resolved] Condition for custom field "image" when not exist!

This support ticket is created 5 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: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 5 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1118306
Screenshot_2018-10-01 Beitrag bearbeiten ‹ Academia Superior – Gesellschaft für Zukunftsforschung — WordPress.png

Hello.

One of our custom fields is "bild". Its an image .
all works great with the following code..

[wpv-conditional if="(!empty($(bild)) )"]
[get_media_info types field='bild' size='full' align='center' separator='<br>']<br>
[/wpv-conditional]

The problem is that the condition only checks if the field has a content. If the image was subsequently deleted, therefore no longer exists, this gives problems in the view.

Therefore, we need a check whether the file exists at all.

Thank you!

#1119747

Hi there,

Thank you for contacting us and I'll be happy to assist.

There is no builtin shortcode or function available to check whether a particular file exists on the server or not. But you can register a custom shortcode to check this, for example:


// custom shortcode to check if bild image exists on the server
add_shortcode('check_bild_image', 'check_bild_image_fn');
function check_bild_image_fn() {
  // very important to validate those
  $bild_image_url = types_render_field("bild", array("output" => "raw"));

  // perform CURL processing to check if image file exists at the URL in $bild_image_url
 
  if ($bild_image_url_exists) {
   // return 1 if the file exists
   return '1';
  }
  else {
   // return 0 if the file exists
   return '0';
  }
}

The above code to register a new custom shortcode can be added at the bottom of active theme’s “functions.php” file and depending on your server configuration, you can add your own processing to check if the file actually exists on the server (look for the comment line “// perform CURL processing to check if image file exists at the URL in $bild_image_url”).

Next, please register this new shortcode [check_bild_image] with Toolset, so it can be used inside its built-in shortcode, as explained in the following guide:
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

After that, you’ll be able to use the updated condition as:


[wpv-conditional if="( [check_bild_image] eq '1' )"]

// content to show when the image exists

[/wpv-conditional]

I hope this helps! Please let us know if you need any further assistance.

#1119909

Thank you so much!
I have now added the function in the functions.php

Unfortunately, nothing is displayed, even if the image exists !

Are you sure that this is correct?
The variable bild_image_url_exists does not exists!!

 if ($bild_image_url_exists) {
   // return 1 if the file exists
   return '1';
  }
  else {
   // return 0 if the file exists
   return '0';
  }

I also tried it with

if (file_exists($bild_image_url)) { ... 

but i think the problem is the path and "if file exists" does not work in that way.

All images are saved in the folder uploads/

In the Widget now thats the new part

[wpv-conditional if="([check_bild_image] eq '1' )"]
[get_media_info types field='bild'   size='full' align='center' separator='<br>']<br>
[/wpv-conditional]

Best regards
Patrick Schauer
Media Data IKT

#1120771

Hi Patrick,

Thanks for writing back and I apologize for not being more clear in my last message.

The code block that I shared was just to give you an overview or outline of how a custom shortcode can be used for this requirement.

The comment line " // perform CURL processing to check if the image file exists at the URL in $bild_image_url ", indicates where the actual processing will need to be done to check for the file, based on the server's configuration.

Using the CURL is considered the most reliable method for checking file's existence, but you'll have to consult your web host first, to make sure that this extension is enabled on the server.

Following are some useful resources on the topic:
https://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php
hidden link

After the processing you can store true or false value in variable "$bild_image_url_exists" and the shorcode will work.

Hope this clarifies further and please let us know if there's anything else we can help you with.

regards,
Waqar

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