1

In the attachments edit page i'm trying to use the selected thumbnail instead of the default icon. 99% of the way there but i can't get the attachment's id into the function.

Am i missing something obvious? Help appreciated! Dc.

function set_attachments_icon($icon, $mime = null, $post_id){
    $vars = get_defined_vars();
    print_r($vars);

    echo " ??".$icon."?? ".$post->id.' '.$post_id.'  ?? id not getting picked up?';

    $thumb_id = get_post_meta($post->ID,'dsv_thumbnail_id', true);

    if($thumb_id!=''){
        $image_attributes = wp_get_attachment_image_src( $thumb_id );
        $icon =  $image_attributes[0];
    }

    return $icon;
}

add_filter('wp_mime_type_icon', 'set_attachments_icon');
apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id );

2 Answers 2

2

First, you don't need the apply_filters below. That is called directly from the wp_mime_type_icon() function already.

The issue is that your add_filter() call is missing the $accepted_args parameter. The add_filter() function has the following construct:

add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1)

So the $accepted_args is 1 by default. Because of this, only the first argument of $icon gets passed into your function. You need to use the following code when adding your filter:

add_filter('wp_mime_type_icon', 'set_attachments_icon', 10, 3);
2
0

The apply_filters call is as follows:

apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); // Last arg is 0 if function pass mime type.

It looks like this is passing the post_id directly and not the post object. That sure would do it! Try this code instead (note that I didn't try it, we're just shooting from the hip here)

function set_attachments_icon($icon, $mime, $post_id){
    if (!$post_id) return $icon;
    $thumb_id = get_post_meta($post_id,'dsv_thumbnail_id', true);
    if($thumb_id){
        $image_attributes = wp_get_attachment_image_src( $thumb_id );
        $icon = $image_attributes[0];
    }
    return $icon;
}
add_filter('wp_mime_type_icon', 'set_attachments_icon', 10, 3);

For me, however, this didn't get hit in the chain because of image_downsize in wp-includes/media.php line 607. You can intercept this to do what you need to do too.

function set_attachments_downsize($return, $attachment_id, $size){
    if ( $size == 'thumbnail' && $thumb_id = get_post_meta($attachment_id,'dsv_thumbnail_id', true) ) {
        $image_attributes = wp_get_attachment_image_src( $thumb_id );
        return $image_attributes[0];
    }
    return $return;
}
add_filter('image_downsize', 'set_attachments_downsize', 10, 3);

Cheers~

11
  • thanks Matthew - updated the above with the apply_filters but no joy.
    – v3nt
    Commented Jan 25, 2012 at 14:19
  • Ok, I added more code to my post. Try that out and let me know! We'll get this sorted Commented Jan 25, 2012 at 14:32
  • agh - still not coming through. How annoying. Do we not need the apply filters for your new code?
    – v3nt
    Commented Jan 25, 2012 at 14:48
  • And your post has the custom meta dsv_thumbnail_id set? Commented Jan 25, 2012 at 14:53
  • yeh - which i can't get without the post or attachments id ;-/
    – v3nt
    Commented Jan 25, 2012 at 14:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.