Skip Navigation

[Resolved] Display Types image field meta

This support ticket is created 6 years, 2 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
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)

This topic contains 6 replies, has 2 voices.

Last updated by thomasS-11 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1111539

Hi.

I'm currently developing a gallery for my customer.
I need to extract some meta data from the image.
I need to be able to extract the image size (without path), and the title.
Currently i cannot see how i can achieve this with Types.

The field i am using is the Types image field, not the Featured Image field.

Please advice.

#1111731

Hi, Toolset doesn't currently provide a shortcode option for getting image dimensions or filename information. I have a custom shortcode that might help. Add this to your snippets in Toolset > Settings > Custom Code, or to your child theme's functions.php file:

add_shortcode( 'toolset_get_image_meta', 'toolset_get_image_meta_func');
function toolset_get_image_meta_func($atts)
{
  global $wpdb;
  $a = shortcode_atts( array(
    'url' => '',
    'meta' => 'filename',
  ), $atts );
  $return = '';
  $url = $atts['url'];
  $id = $wpdb->get_var( $wpdb->prepare ("SELECT ID FROM $wpdb->posts WHERE guid=%s ORDER BY `ID` DESC", $url ) );
  $attachment_meta = get_post_meta( $id, '_wp_attachment_metadata', true );
  switch( $a['meta'] ) {
    case 'filename':
      if (isset($attachment_meta['file']))
        $return = basename ( $attachment_meta['file'] );
      break;
    case 'height':
      if (isset($attachment_meta['height']))
        $return = $attachment_meta['height'];
      break;
    case 'width':
      if (isset($attachment_meta['width']))
        $return = $attachment_meta['width'];
      break;

    default:
  }
  return $return;
}

Use the shortcode in wp-admin like this:

Field: [wpv-post-field name='wpcf-single-img']<br />
Filename: [toolset_get_image_meta url="[wpv-post-field name='wpcf-single-img']" meta="filename"][/toolset_get_image_meta]<br />
Height: [toolset_get_image_meta url="[wpv-post-field name='wpcf-single-img']" meta="height"][/toolset_get_image_meta]<br />
Width: [toolset_get_image_meta url="[wpv-post-field name='wpcf-single-img']" meta="width"][/toolset_get_image_meta]<br />

Replace "wpcf-single-img" with the slug of your custom field. Types custom fields use the prefix "wpcf-" in the database, so it should be added here as shown.

This shortcode relies on the guid field in the posts table, and expects that value to match the custom field value. Generally this is reliable unless you migrate between different site domains without modifying GUIDs in the posts table. If your custom field values and image guids don't match, this solution won't work and you'll need a different approach.

#1111759

Thanks alot. i will try this.

I don't see how i am able to get the medium size with this shortcode. Isn't it only for the full size image?

#1111766

Also, would this work with a repeatable group image?

#1111811

Sorry for the multiple questions, but can the shortcode be simplified?
Currently, i cannot use it for data-attributes in HTML, because it is using both double quotes and single quotes.
I am trying to add: data-size="WIDTHxHEIGHT", but it outputs the full code.

If im not rendering it inside data-size attribute, it renders correct.

*UPDATE*

I fixed it by making a shortcode that uses your shortcode;

add_shortcode('types_get_meta_data','types_get_meta_data');

function types_get_meta_data ($atts) {

  extract(shortcode_atts(
    array('id' => '',
          'data' => '' 
         ), $atts)
         );
  
  $url = do_shortcode("[wpv-post-field name='$id']");
  
  return do_shortcode("[toolset_get_image_meta url='$url' meta='$data'][/toolset_get_image_meta]");

}

This allows for me to use: [types_get_meta_data id="wpcf-field-name" data="width"] data can contain width, height or filename

#1112258

It looks like you were able to create a separate shortcode to do what you want. Is this ticket resolved? If not, please clarify which questions you need answered.

#1112677

My issue is resolved now. Thank you!