Make WordPress Core

Opened 7 years ago

Last modified 4 years ago

#18951 closed feature request

A function to add default images for themes — at Version 5

Reported by: F J Kaiser Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.2.1
Component: Themes Keywords: has-patch
Focuses: Cc:

Description (last modified by scribu)

Adding default images to themes is not an easy task. They have to play nicely with different image sizes that are built-in and/or added via add_image_size();. If they don't, then they will simply break the layout or won't change with user changes. Sadly we can't simply use most of the core media/image functions as those functions check if the image is an attachment image and - if not - abort.

So I want to introduce wp_default_img();. It works with an input array of attributes and offers two filters (wp_default_img_attr & wp_default_img). So setting up default images is as easy as using a filter (if the theme developer isn't satisfied with the functions default args) and finally just adding

// functions.php during init:
add_image_size( 'default_img', 80, 80, true );

// Inside some template
$placeholder = get_site_url( null, 'your_path' ).'/some_img.jpg';
echo wp_default_img( array( 'url' => $placeholder, 'size' => 'default_img' ) );

The function also cares about cropping images if 4th argument set to true when registering the size using add_image_size();.

Related ticket that pops the whole image size data into get_intermediate_image_sizes();, can be found here #18947.

Change History (5)

#1 @F J Kaiser
7 years ago

  • Cc 24-7@… added

#2 @F J Kaiser
7 years ago

Accidently posted the old (not tested) version in the describtion. Here's the working one.

#3 @F J Kaiser
7 years ago

Found a bug within title attribute. New Version.

#4 @F J Kaiser
7 years ago

Still had wrong single/double quotes. (Hopefully) final paste.

It would also be handy to have a sidekick function that cares about storing the default images as associative array with names as keys and urls as values. So calling the image inside templates would be even more easy:

function wp_set_default_img( $name, $url )
	return $GLOBALS['_wp_default_images'][ $name ] = $url;

#5 @scribu
7 years ago

  • Description modified (diff)

That's why you shouldn't post long blocks of code directly in the description.

You should use trac attachments instead.

Note: See TracTickets for help on using tickets.