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

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.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest