Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 );
share|improve this question
add comment

2 Answers

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~

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

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);
share|improve this answer
    
ok - think i spoke too soon. All the data seems to be correct now but its not changing the icon (the src is empty). Here's the spitout. Array ( [icon] => sandbox.modernactivity.co.uk/waspface_wp/wp-content/uploads/… [mime] => video/mp4 [post_id] => 8 [thumb_id] => 16 [image_attributes] => Array ( [0] => sandbox.modernactivity.co.uk/waspface_wp/wp-content/uploads/… [1] => 150 [2] => 84 [3] => 1 ) ) –  daniel Crabbe Jan 25 '12 at 16:45
    
It may be because you're using $post->ID instead of $post_id as the get_post_meta() parameter. –  prettyboymp Jan 25 '12 at 17:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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