Wordpress wp_get_attachment_metadata

I have a problem with some of my custom gallery functions, that I have used for years in WordPress. Now with WordPress Version 4.0 the wp_get_attachment_metadata function isn't working correctly, so that I have no alt-attribute. Everything else is working perfectly. Images are visible and in correct order.

<!-- language: lang-php -->
/* CUSTOM GALLERY ############################################### */
add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr) { global $post; if (isset($attr['orderby'])) { $attr['orderby'] = sanitize_sql_orderby($attr['orderby']); if (!$attr['orderby']) unset($attr['orderby']); } extract(shortcode_atts(array( 'order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post->ID, 'itemtag' => 'dl', 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, 'size' => 'thumbnail', 'include' => '', 'exclude' => '' ), $attr)); $id = intval($id); if ('RAND' == $order) $orderby = 'none'; if (!empty($include)) { $include = preg_replace('/[^0-9,]+/', '', $include); $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby)); $attachments = array(); foreach ($_attachments as $key => $val) { $attachments[$val->ID] = $_attachments[$key]; } } if (empty($attachments)) return ''; $output = "<div class=\"slideshow-wrapper\">\n"; $output .= "<div class=\"preloader\"></div>\n"; $output .= "<ul data-orbit>\n"; foreach ($attachments as $id => $attachment) { $img = wp_get_attachment_image_src($id, 'full'); $image_data = wp_get_attachment_metadata($id, FALSE ); $output .= "<li>\n"; $output .= "<img src=\"\" data-src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"{$image_data['image_meta']['title']}\" class=\"gallery-img\"/><noscript><img src=\"{$img[0]}\" data-src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"{$image_data['image_meta']['description']}\" class=\"gallery-img\"\></noscript>"; $output .= "</li>\n"; } $output .= "</ul>\n"; $output .= "</div>\n"; return $output;
}
0

2 Answers

I found a solution by myself. Instead of using the wp_get_attachment_metadata, i'm now using the wp_prepare_attachment_for_js function.

/* CUSTOM GALLERY ############################################### */
add_filter('post_gallery', 'my_post_gallery', 10, 2);
function my_post_gallery($output, $attr) { global $post; if (isset($attr['orderby'])) { $attr['orderby'] = sanitize_sql_orderby($attr['orderby']); if (!$attr['orderby']) unset($attr['orderby']); } extract(shortcode_atts(array( 'order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post->ID, 'itemtag' => 'dl', 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, 'size' => 'thumbnail', 'include' => '', 'exclude' => '' ), $attr)); $id = intval($id); if ('RAND' == $order) $orderby = 'none'; if (!empty($include)) { $include = preg_replace('/[^0-9,]+/', '', $include); $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby)); $attachments = array(); foreach ($_attachments as $key => $val) { $attachments[$val->ID] = $_attachments[$key]; } } if (empty($attachments)) return ''; $output = "<div class=\"slideshow-wrapper\">\n"; $output .= "<div class=\"preloader\"></div>\n"; $output .= "<ul data-orbit>\n"; foreach ($attachments as $id => $attachment) { $img = wp_prepare_attachment_for_js($id); $url = $img['sizes']['full']['url']; $height = $img['sizes']['full']['height']; $width = $img['sizes']['full']['width']; $alt = $img['alt']; $output .= "<li>\n"; $output .= "<img src=\"\" data-src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"{$alt}\" class=\"gallery-img\"/> <noscript><img src=\"{$url}\" data-src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"{$alt}\" class=\"gallery-img\"\></noscript>"; $output .= "</li>\n"; } $output .= "</ul>\n"; $output .= "</div>\n"; return $output;
}
0

In your first <img> tag you're using $image_data['image_meta']['title'] for the alt attribute. In the second <img> tag (the one in the <noscript> tag you're using $image_data['image_meta']['description'], not [title]. wp_get_attachement_metadata() doesn't have a [image_meta][description] value.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like