Support » Developing with WordPress » Explanation requested for function to display ico file

  • Resolved Guido

    (@guido07111975)


    Hi,

    I use code below to display the ico file in media gallery, instead of the native placeholder.

    Found a code snippet here and created this:

    
    function display_ico( $icon, $mime, $post_id ) {
    	if( $src = false || 'image/x-icon' === $mime && $post_id > 0 )
    	$src = wp_get_attachment_image_src( $post_id );
    	return is_array( $src ) ? array_shift( $src ) : $icon;
    }
    add_filter( 'wp_mime_type_icon', 'display_ico', 11, 3 );
    

    I don’t fully understand the meaning of certain parts..

    This part: if( $src = false
    Guess this means if src of ALL types of files is false, so not only the src of an ico file?

    And this part: is_array( $src ) ? array_shift( $src ) : $icon;
    If src is an array, remove the first element from this array OR return the icon?

    Who can help me with this?

    Guido

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Hiya Guido,

    All if( $src = false is really doing is assigning false to $src. Because the returned result of the assignment is always false, it has no impact on the remaining conditional’s OR logic. The result of 'image/x-icon' === $mime && $post_id > 0 is wholly responsible for determining what the if conditional does.

    The author could have done the assignment on a separate line and let the conditional only examine what really matters — that we are dealing with an image and an actual post. It would have been more clear. I personally dislike these sorts of “clever” shortcuts. They generally are hardly more efficient and are much more confusing.

    As for the other line you are not sure of, your plain English interpretation is more or less correct, albeit imprecise. The ternary operators ( ? : ) are another shortcut for a “if then else” logic structure. Ternary operators have their place. Conditionally returning one value or another is a legitimate use, so I can’t complain about this shortcut, though it would have been clearer to use a traditional if(){}else{} structure.

    array_shift() alters the passed array by removing the first element and returning that element. In this usage it’s superfluous to alter $src, there is no point in doing so. It would have been more clear and efficient to simply do $src[0]

    The entire line is basically saying that if $src is an array, return $src[0], otherwise return $icon.

    Now that we’ve seen the entire process, there was no reason at all to assign false to $src. wp_get_attachment_image_src() will always return either false or an array of image data. Needless confusion and obfuscation.

    Guido

    (@guido07111975)

    Great, thanks mate 🙂

    So something like this is safe to use as well?

    
    function display_ico( $icon, $mime, $post_id ) {
    	if( 'image/x-icon' === $mime && $post_id > 0 ) {
    		$src = wp_get_attachment_image_src( $post_id );
    		$icon = $src[0];
    	}
    	return $icon;
    }
    add_filter( 'wp_mime_type_icon', 'display_ico', 11, 3 );
    

    Why make it more difficult??

    Guido

    Moderator bcworkz

    (@bcworkz)

    🙂 It’s probably fine. What if wp_get_attachment_image_src( $post_id ) should return false? You’ll want to return the passed $icon unchanged. I’m not sure it’s possible to get false when'image/x-icon' === $mime. So you are probably fine as is, but address the false possibility if you want to be totally safe.

    Guido

    (@guido07111975)

    Good point, better to be safe 🙂

    
    if( $src != false ) {
    	$icon = $src[0];
    }
    

    Thanks!

    Guido

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.