Make WordPress Core

Opened 13 years ago

Closed 10 years ago

Last modified 10 years ago

#18951 closed feature request (wontfix)

A function to add default images for themes

Reported by: f-j-kaiser's profile 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.

Attachments (1)

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

Download all attachments as: .zip

Change History (20)

#1 @F J Kaiser
13 years ago

  • Cc 24-7@… added

#2 @F J Kaiser
13 years ago

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

#3 @F J Kaiser
13 years ago

Found a bug within title attribute. New Version.

#4 @F J Kaiser
13 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
13 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.

#6 @marfarma
13 years ago

  • Cc marfarma added

@F J Kaiser
13 years ago

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

#7 @F J Kaiser
13 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.

#8 @scribu
13 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.

http://wpdevel.wordpress.com/version-3-3-project-schedule/

#9 @scribu
13 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.

#10 @F J Kaiser
13 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.

#11 @scribu
13 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."

#12 @F J Kaiser
13 years ago

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

#13 @scribu
13 years ago

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

#14 @chriscct7
10 years ago

  • Keywords dev-feedback added

#15 @obenland
10 years ago

  • Keywords reporter-feedback added; dev-feedback removed

I'm not sure I understand the purpose of the function? What would be a use case for it?

#16 @chriscct7
10 years ago

I'm not too sure

#17 @F J Kaiser
10 years ago

  • Keywords reporter-feedback removed

A theme that uses post thumbnails would force the user to always have a post thumbnail - or break layout. The proposed function would make that much easier for theme authors. The other way currently is to add a pretty complicated callback to the get_{$meta_type}_metadata filter (explanation or example on demand).

Use case/Example:

if ( have_posts() ) {
    while ( have_posts() ) {
        the_posts();

        the_title();
        if ( has_post_thumbnail() ) {
            the_post_thumbnail();
        }
        else {
            wp_default_img();
        }
        the_content();
    }
}

One of the side effects that I could see is that theme shops or marketplace authors would have less questions of the following sort by users

"I installed the theme. Why doesn't it look like the demo?"

Last edited 10 years ago by F J Kaiser (previous) (diff)

#18 follow-up: @johnbillion
10 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I don't think this provides any real value.

This is perfectly sufficient:

if ( has_post_thumbnail() ) {
    the_post_thumbnail();
} else {
    echo '<img src="' . get_stylesheet_directory() . '/default.jpg">';
}

#19 in reply to: ↑ 18 @F J Kaiser
10 years ago

Replying to johnbillion:

I don't think this provides any real value.

...aside from taking image sizes into account. Whatever, I'm not going to argue on that as I know my way around.

Note: See TracTickets for help on using tickets.