Support » Fixing WordPress » Default thumbnail in functions.php?

  • OK, here’s something I’m trying to figure out….. Is there a way to specify a default thumbnail in my functions.php file?

    Currently I have this (in functions.php):

    // THIS IS THUMBNAIL SUPPORT
    if ( function_exists( 'add_theme_support' ) ) { // Added in 2.9
    	add_theme_support( 'post-thumbnails' );
    	set_post_thumbnail_size( 50, 50, true ); // Normal post thumbnails
    	add_image_size( 'single-post-thumbnail', 300, 999 ); // Permalink thumbnail size
             add_image_size( 'title-image', 50, 27 ); // Title mini photo
    }
    
    // THIS LINKS THE THUMBNAIL TO THE POST PERMALINK
    
    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
    
    function my_post_image_html( $html, $post_id, $post_image_id ) {
    
    	$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
    
    	return $html;
    }

    to define my thumbnail, and to link it to the permalink

    now I use the thumbnail and it’s various iterations throughout my theme. So atm, I use this to swap in a default image in one of the locations (index.php):

    <?php
    if ( has_post_thumbnail() ) {
         the_post_thumbnail('title-image', array('class' => 'titleImage', 'alt' => 'Title Icon'));
         } else {
         echo "<img src='" . get_bloginfo('template_directory') .
        "/images/defaultIcons/voodooTitleDefault.png' alt='Default Title Icon' title='Default Title Icon' class='titleImage' />"; }
    ?>

    which works fine enough. But I have to include that buncha code in each of 4 or 5 locations to get a default image. Plus I use the thumbnail on my main (non-WP) page…it’s getting to be a pain to use all that. Plus the if/else I have above doesn’t include the permalink link.

    So…..is there a way for me to specify the default thumbnail one time in functions.php, so all of my thumbnail uses throughout my theme and beyond will have the default available if I don’t specifiy one?

    Thanks much in advance!

Viewing 15 replies - 1 through 15 (of 29 total)
  • yup….I’m definitely in over my head on this one…. And…I think I just found the end of google.

    I’ve tried a lot of cut & paste of various php stuff…..thing is I’m just not so bright at this stuff.

    I found this:
    http://wordpress.org/extend/plugins/default-post-thumbnails

    But it’s not working….as I get a default thumbnail just dropped into my posts….not at my specified sizes, with my classes, in the multiple places I have set up, etc

    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
    
    function my_post_image_html( $html, $post_id, $post_image_id ) {
    
    	$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
    
    	return $html;
    }

    Looking at your filter above, it would appear(i’m not certain) to me that when no image is returned, you’ll actually still be getting a link output(the link in your filter will still output even when $html is set to an empty to string).

    You could do a very simple condition in the filter..

    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
    
    function my_post_image_html( $html, $post_id, $post_image_id ) {
    	if( '' == $html )
    		$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '"><img src="defaultimageblahblah" /></a>';
    	else
    		$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
    	return $html;
    }

    I could be wrong about the output though, i’m not sure..
    [1] – http://core.trac.wordpress.org/browser/trunk/wp-includes/post-thumbnail-template.php

    I’m assuming at this point that when no thumbnail exists, get_the_post_thumbnail[1](scroll down to the bottom – last function) still returns the $html value set as an empty string, when no thumbnail exists. Of course my assumption could be wrong.

    Can you confirm?

    hmmm….no dice it seems. I messed up the url to my image the first time, which totally blew my site up (no css, no admin)

    So I tweaked it….which let me back in….but with a correct url, I get nothing happening differently

    Can you confirm?

    I’m not sure what to confirm? Sorry….. I’m at the edge of my knowledge here. I do know that the thumbnails put into my index.php by the_post_thumbnail has the permalink assigned to them. The alternate ‘default’ icon assigned by the if/else statement in index.php does not have any permalink attached (as expected). Same hold true with the new code dropped into functions.php, and no additional images were spit out anywhere in my page and nothing appeared in my source to make me think anything was going on differently.

    Thanks so far!!

    Ok looks like i’ll have to test the code… 😉

    Will reply once i’ve had a chance to test.. 🙂

    Ok, turns out that the filter change i suggested before does work, but you’ll need to make a change to how you’re calling the post thumbnail..

    Remove this condition..

    if ( has_post_thumbnail() ) {

    Whenever a post doesn’t have a post thumbnail, the function the_post_thumbnail will return an empty string, when it returns an empty string, you can catch it with the filter and return something else. By using that condition, the empty string case can’t be met.

    So if you take this code.

    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
    
    function my_post_image_html( $html, $post_id, $post_image_id ) {
    	if( '' == $html )
    		$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '"><img src="defaultimageblahblah" /></a>';
    	else
    		$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
    	return $html;
    }

    And remove the condition around the_post_thumbnail it will work.. (adjust the example image code of course).

    NOTE: There’s nothing wrong with *not checking* if there’s a thumbnail with has_post_thumbnail, because the_post_thumbnail returns empty on fail(so you won’t get any errors or anything like that).

    Essentially all this means is, that instead of doing this everywhere…

    <?php if ( has_post_thumbnail() ) the_post_thumbnail(); // I'm only shown when i'm not empty ?>

    You now just do..

    <?php the_post_thumbnail(); // I'm an empty string when i fail ?>

    Which then allows you to filter the function and return a default image when it’s empty… 🙂

    OK, we gettin somewhere! Thanks for sticking around so far!! So now we have the default image getting dropped in wherever I use the_post_thumbnail…..but……they all come up the same size, and totally bypass any further modification. Which I guess means there’s something going on here that’s still above my paygrade….

    I’m guessing it’s because we are appending the html output after the thumbnail sizes are set….and dropping in an image url…so it’s just affecting the actual url output and has nothing to do with the original sizes I set up?

    So here’s the total code I have in functions.php….

    // THIS IS THUMBNAIL SUPPORT
    
    	add_theme_support( 'post-thumbnails' );
    	set_post_thumbnail_size( 50, 50, true ); // Normal post thumbnails
    	add_image_size( 'single-post-thumbnail', 300, 999 ); // Permalink thumbnail size
            add_image_size( 'title-image', 50, 27 ); // Title mini photo
    
    // THIS LINKS THE THUMBNAIL TO THE POST PERMALINK AND DROPS IN A DEFAULT IMAGE
    
    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
    
    function my_post_image_html( $html, $post_id, $post_image_id ) {
    	if( '' == $html )
    		$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '"><img src="' . get_bloginfo('template_directory') . '/images/voodooplate.png" /></a>';
    	else
    		$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
    	return $html;
    }

    And I have 3 iterations of the_post_thumbnail in theme like so:

    <?php the_post_thumbnail('title-image', array('class' => 'titleImage', 'alt' => 'Title Icon')); ?>
    <?php the_post_thumbnail(array( 75,75,true ), array('class' => 'postImage', 'alt' => 'Post Thumbnail')); ?>
    <?php the_post_thumbnail('single-post-thumbnail', array('class' => 'pageImage', 'alt' => 'Full Page Image')); ?>

    so now….the multiple image sizes, the classes being appended in, etc, are ignored by the default. The sizes and classes of course are important for the layout. Is there a way to get the default thumbnail to play nice?

    Again, thanks a lot…. I’m quickly learning

    Seems to be a bug of some kind, if i echo the $size directly in wp-includes/post-thumbnail-template.php it’s there as expected, yet if i add the size into the filter, it’s unable to see the size.

    The size should be available to the filter, here’s the filter hook for reference.

    return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr );

    You see size there, and it’s fed a valid value inside the the_post_thumbnail function, yet it seems to be empty whenever i try to print it out(or do whatver with it) inside a filter.

    For ref: (opening line of function i’m using)

    function my_post_image_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {

    The problem can easily be confirmed by doing the following inside the filter function.

    print '<pre>';print_r( get_defined_vars() );print '</pre>';

    This is what i see.

    Array
    (
        [html] => image-code-is-shown-here
        [post_id] => 12
        [post_thumbnail_id] => 15
    )

    Values are examples.

    When what i’m expecting to see is..

    Array
    (
        [html] => image-code-is-shown-here
        [post_id] => 12
        [post_thumbnail_id] => 15
        [size] => the-size-value
        [attr] => any-attrs-given
    )

    Again values are examples.

    I’m not sure what i can suggest, the size never seems to be there, whether returning the default image or an actual post thumbnail, it’s as if it vanishes somewhere along the way, when really it should be there (unless there’s a mistake in my understanding of the filter hook – anyone is welcome to correct me).

    oooh. That’s kind of a bummer! I was wondering where the extra ‘stuff’ was going….

    Thanks for taking the time on this one! It’d definitely be cool if the values didn’t run away……saves a lot of time/code on implementing default thumbnails in multiple areas of a theme….especially when adding in classes, permalink links, etc…..

    I mean, its not so hard to set up using 4 or 5 different images as defaults….presized and everything, I just liked this idea much more!

    Anyway, thanks much for trying/testing!

    It could be that i’m misunderstanding the filter hook available though, so it may simply be that size isn’t available as i would have expected.

    It would be a little strange though, that two variables are passed to the hook if they’re not intended to be available, again i might just misunderstand the usage.

    Oh god, i’m such a fool, i’ve just spotted my silly error, it’s here.

    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

    The last parameter is how many args/vars/parameters your filter will accept.. (there’s 5 available on this particular hook).

    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 5 );

    And voila $size is now available to the function… 🙂
    So you can do what you want inside the filter based on what’s in that var now… 🙂

    Setting to 4 or 5 will allow you to access $size inside the filter.

    😀

    Ya know….I copied and pasted that bit…..and forgot to ever really figure out what the numbers were for

    Thank you so much! I’m gonna play a bit and report back!

    First number is the priority and second is the supported args.

    The priority isn’t necessarily always important, but can be useful when you’re interacting with plugins or other functions that also run filters on the same function (you may want one to have precedence over the other for example).

    Just happy we found the problem… 🙂

    OK…sooooo (sorry to be needy!)

    how to use $size?

    I’ve just cruised the codex….looking at any sample code for attachments and whatnot (nothing is detailed enough for me to glean usage)….I’m just not following how to use the $size here:

    function my_post_image_html( $html, $post_id, $post_image_id, $size, $attr ) {
    	if( '' == $html )
    		$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '"><img src="' . get_bloginfo('template_directory') . '/images/voodooplate.png" /></a>';

    Guess I’m really kinda out of my element here. Like does $size specifiy what size image to use based on the settings I have further up in functions.php (the set_post_thumbnail_size, add_image_size stuff)?

    Also, I just noticed, in the filter you reference $post_thumbnail_id and I reference $post_image_id Is that an issue?

    As far as i can see size contains the name you declared in the call to the post thumbnail, ie. title-image, single-post-thumbnail and so on…

    It does not however contain the width and height values. If you want them, i’ve found a way to get them… 😉

    Inside your filter..

    // ignore - forum first line indent bug fix
    	global $_wp_additional_image_sizes;
    
    	if( isset( $_wp_additional_image_sizes[$size] ) ) {
    		$width = $_wp_additional_image_sizes[$size]['width'];
    		$height = $_wp_additional_image_sizes[$size]['height'];
    	}

    And now $width and $height have the values that apply to the given size being called..

    I guess I’m not sure how to actually use the $size…..

    where do I plop it into the function to get the image to use the size?

    like it’s in here..
    function my_post_image_html( $html, $post_id, $post_image_id, $size, $attr ) {

    but it goes somewhere else to actually apply size/declared name to the image?

    (btw, that height and width thing is nice….I’m studying that….)

Viewing 15 replies - 1 through 15 (of 29 total)
  • The topic ‘Default thumbnail in functions.php?’ is closed to new replies.