Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#18951 new feature request

A function to add default images for themes

Reported by: F J Kaiser Owned by:
Milestone: Future Release 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.

Attachments (1)

18951.patch (4.3 KB) - added by F J Kaiser 3 years ago.
Draft of wp_default_img() inside /wp-includes/media.php

Download all attachments as: .zip

Change History (14)

comment:1 F J Kaiser3 years ago

  • Cc 24-7@… added

comment:2 F J Kaiser3 years ago

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

comment:3 F J Kaiser3 years ago

Found a bug within title attribute. New Version.

comment:4 F J Kaiser3 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;

comment:5 scribu3 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.

comment:6 marfarma3 years ago

  • Cc marfarma added

F J Kaiser3 years ago

Draft of wp_default_img() inside /wp-includes/media.php

comment:7 F J Kaiser3 years ago

  • Keywords has-patch added
  • Version changed from 3.2.1 to 3.3

@scribu I know. Just had no setup until today.

Ad wp_default_img();: I'm not sure if it's the best idea to filter the defaults that late. I did it to allow full controle of the complete attribute, so you could add additional (non-default) attributes along for ex. the alt or title attribute. The problem with doing so is that I added the leading space for an attribute before the filtering. If now someone forgets to add the leading space, the outcome could be funny.

The content of the size check is independent of the outcome of #18947, but I already added a patch there to get this on the road as quick as possible.

comment:8 scribu3 years ago

  • Milestone changed from Awaiting Review to Future Release

It's too late for enhancements for 3.3, especially when they don't even have a clear implementation yet.


comment:9 scribu3 years ago

  • Version changed from 3.3 to 3.2.1

Sorry, I thought you set the milestone.

See http://codex.wordpress.org/Reporting_Bugs#Ticket_Fields for the difference between milestone and version.

comment:10 F J Kaiser3 years ago

n/p. I found the description for the ticket fields a little confusing when it comes to enchancements. "The version of WordPress where the bug was found": I added 3.3 as it's still not present there. Thanks for helping out.

comment:11 scribu3 years ago

Yeah; updated codex: "The earliest version of WordPress where the bug was found or the current stable version for enhancements and feature requests."

comment:12 F J Kaiser3 years ago

@scribu Btw: "Milestone" & "Priority" is disabled for me (seem to not have enough privileges/capabilities).

comment:13 scribu3 years ago

Yeah, that's to prevent people from cluttering the current milestone.

Note: See TracTickets for help on using tickets.