Home » Wordpress » How can I replace TimThumb with WordPress native image tools?

How can I replace TimThumb with WordPress native image tools?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have recently started work on an existing WordPress site. The theme on the WordPress install uses TimThumb excessively, and it really is unnecessary. WordPress native image sizes should be able to handle the functionality required. However, I am not sure how to replace TimThumb.

There is a post-thumb.php in the theme file that generates the thumbnails. The file is as follows:

if (get_option('solostream_default_thumbs') == 'yes') { $defthumb = get_bloginfo('stylesheet_directory') . '/images/def-thumb.jpg'; } else { $defthumb == 'false'; }

$solostream_img = get_the_image(array(
    'meta_key' => 'thumbnail',
    'size' => 'thumbnail',
    'image_class' => 'thumbnail',
    'default_image' => $defthumb,
    'format' => 'array',
    'image_scan' => true,
    'link_to_post' => false, ));

if ( $solostream_img['url'] && get_option('solostream_show_thumbs') == 'yes' && get_post_meta( $post->ID, 'remove_thumb', true ) != 'Yes' ) { ?> 

    <img class="<?php echo $solostream_img['class']; ?>" src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $solostream_img['url']; ?>&amp;w=150&amp;h=150&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />

<?php } 

} ?>

I just want to replace this entire function with WordPress’ built in the_post_thumbnail('thumbnail') function.

Is it as simple as replacing

src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $solostream_img['url']; ?>&amp;w=150&amp;h=150&amp;zc=1"

with something like

src="<?php the_post_thumbnail('thumbnail'); ?>"

Any ideas or a push in the right direction would be appreciated. Thanks for reading.

Answers:

You can add custom thumbnails and use them within your code.

First you have to add theme support, set the thumbnail name, set the size, and set the size

add_theme_support( 'post-thumbnails' ); // this enable thumbnails and stuffs
add_image_size( 'mini-thumbnail', 60, 60, true );
add_image_size( 'one-more-size', 300, 260, true );

You can then use this in your code by calling the following:

<?php while ( have_posts() ) : the_post();
the_title();
the_post_thumbnail( 'mini-thumnail' );
the_excerpt(); ?>

We have implemented this. I used the following resource for reference when we implemented and to get more details for specific actions: http://carlorizzante.com/2011/you-dont-need-timthumb-use-add_image_size-for-thumbnails/ (I’m not related to the site)

Questions:
Answers:

I’m not sure if it’s the most efficient way, but I have been able to get it working by replacing

<img class="<?php echo $solostream_img['class']; ?>" src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $solostream_img['url']; ?>&amp;w=150&amp;h=150&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />

with

<?php the_post_thumbnail('thumbnail'); ?>