Default thumb – how to set it

While there’s no such functionality built into core, there’s a Track Ticket I proposed 2 years ago. You can simply rip of the functionality from the suggested wp_default_img() function. I’ll greatly appreciate any support in comments on that ticket, as this would be highly needed for every theme developer. Thanks in advance.

MU-Plugin

The following code is meant to be dropped into your ~/wp-content/mu-plugins folder (or whatever you named it). It will then per default be available in every site on a network install or simply everywhere in your single site install. Use it like this:

default_img( array( /* attributes - see doc block */ ) );

Just save the code below into a new file with any name and you’re ready to go.

<?php
defined( 'ABSPATH' ) OR exit;
/**
 * Plugin Name: Default Image template tag
 * Plugin URI: http://unserkaiser.com
 * Description: Adds a default image template tag
 * Version: 0.1
 * Author: Franz Josef Kaiser
 * Author URI: http://unserkaiser.com
 * License: GNU GPL 2 <https://gist.github.com/1365159>
 */

/**
 * Default image
 *
 * Builds an default <img> for use in themes or plugins before any other images are added.
 * Resizes & crops the image using the built-in (retireved via `get_intermediate_image_sizes();`)
 * or custom image (added via `add_image_size();`) sizes.
 *
 * Retrieves calculated resize dimension @uses image_resize_dimensions();
 * Builds the width and height string @uses image_hwstring();
 *
 * @param $attr
 * @internal param $args (array)
 *        string $url URl to the given default image.
 *        string $size Optional. Default is 'medium'.
 *        string (optional) $alt Image Description for the alt attribute.
 *        string (optional) $title Image Description for the title attribute.
 *        string (optional) $align Part of the class name for aligning the image.
 *        string (optional) $echo Wheter to return or echo the $image
 * @return string HTML IMG element for given image attachment
 */
function default_img( $attr )
{
    // Sizes registered via add_image_size();
    global $_wp_additional_image_sizes;

    $defaults = array(
        'size'    => 'medium',
        'classes' => false,
        'alt'     => '',
        'title'   => '',
        'align'   => 'none',
        'echo'     => true,
    );

    $attr = wp_parse_args( $attr, $defaults );
    $attr = array_map( 'esc_attr', $attr );

    if ( 'thumb' === $attr['size'] )
        $attr['size'] = 'thumbnail';

    // Size in built in sizes - call size setting from DB
    # behavoir in here dependent on @link http://core.trac.wordpress.org/ticket/18947
    # if in core, we change to:
    # $sizes = get_intermediate_image_sizes();
    # $size_data = $sizes[ $size ];
    if ( ! in_array(
        $attr['size'],
        array_keys( $_wp_additional_image_sizes )
        ) )
    {
        # @TODO delete?
        $sizes = get_intermediate_image_sizes();

        // Get option - gladly autoloaded/can use wp_cache_get();
        $size_data['width']  = intval( get_option( "{$attr['size']}_size_w" ) );
        $size_data['height'] = intval( get_option( "{$attr['size']}_size_h" ) );
        // Not sure how this will behave if cropped is false (autoloaded option not added)
        if ( ! $size_data['crop'] = get_option( "{$attr['size']}_crop" ) )
            $attr['crop'] = false;
    }
    // Size array from global registered additional/custom sizes array
    else
    {
        $size_data = $_wp_additional_image_sizes[ $attr['size'] ];
    }

    // Retrieve image width & height
    $img_info  = @getimagesize( $attr['url'] );

    // Calculate final dimensions - if "crop" was set to true during add_image_size(), the img will get ... cropped
    $end_sizes = image_resize_dimensions(
        $img_info[0],
        $img_info[1],
        $size_data['width'],
        $size_data['height'],
        $size_data['crop']
    );

    // defaults to px units.
    // Can't get changed, as applying units is not possible
    $hwstring  = trim( image_hwstring(
        $end_sizes[4],
        $end_sizes[5]
    ) );

    // Attributes:
    // Not made required as users tend to do funky things (...and lock screen readers out)
    ! empty( $attr['alt'] ) AND $attr['alt'] = " alt="{$attr["alt']}'";

    if ( ! $attr['title'] )
    {
        $mime = explode( "https://wordpress.stackexchange.com/", $img_info['mime'] );
        $attr['title'] = sprintf( __( 'default image of type: %1$s' ), ucfirst( $mime[1] ) );
    }

    $attr['title'] = " title="{$attr["title']}'";

    $attr['align'] = " align{$attr['align']}";
    $attr['size']  = " size-{$attr['size']}";

    // Allow filtering of the default attributes
    $attr = apply_filters( 'wp_default_img_attr', $attr );

    // Build class attribute, considering that maybe some attribute was unset via the filter
    $classes  = "class="wp-img-default{$attr["align']}{$attr['classes']}{$attr['size']}'";

    $url   = trim( $attr['url'] );
    $image = "<img src="https://wordpress.stackexchange.com/questions/112787/{$url}" {$hwstring} {$classes}{$attr['alt']}{$attr['title']} />";

    // Allow filtering of output
    $image = apply_filters( 'wp_default_img', $image );

    $attr['echo'] AND print $image;

    return $image;
}