WordPress.org

Ready to get started?Download WordPress

Forums

Top 10 - Popular posts plugin for WordPress
[Plugin: Top 10] Filter for image link (9 posts)

  1. Riccardo
    Member
    Posted 1 year ago #

    Hi,
    I started using the plugin, but wasn't happy with the fact that the post images were not the right size, so I added a filter in the plugin to get a place to customize the url.

    This is the code, if you find it useful you can add it to the next version of the plugin!

    function tptn_get_the_post_thumbnail($postid) {
    
    	$result = get_post($postid);
    	$tptn_settings = tptn_read_options();
    	$output = '';
    
    	if (function_exists('has_post_thumbnail') && has_post_thumbnail($result->ID)) {
    	  $postimage = wp_get_attachment_image_src( get_post_thumbnail_id($result->ID), 'thumbnail');
    	  $postimage = apply_filters( 'tp10_postimage', $postimage[0], $tptn_settings[thumb_width], $tptn_settings[thumb_height] );
    	  $output .= '<img src="'.$postimage.'" alt="'.$title.'" title="'.$title.'" style="max-width:'.$tptn_settings[thumb_width].'px;max-height:'.$tptn_settings[thumb_height].'px;" border="0" class="tptn_thumb" />';
    
    	//	$output .= get_the_post_thumbnail($result->ID, array($tptn_settings[thumb_width],$tptn_settings[thumb_height]), array('title' => $title,'alt' => $title, 'class' => 'tptn_thumb', 'border' => '0'));
    	} else {
    		$postimage = get_post_meta($result->ID, $tptn_settings[thumb_meta], true);	// Check
    		if (!$postimage && $tptn_settings['scan_images']) {
    			preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $result->post_content, $matches );
    			// any image there?
    			if (isset($matches) && $matches[1][0]) {
    				$postimage = $matches[1][0]; // we need the first one only!
    			}
    		}
    		if (!$postimage) $postimage = get_post_meta($result->ID, '_video_thumbnail', true); // If no other thumbnail set, try to get the custom video thumbnail set by the Video Thumbnails plugin
    		if ($tptn_settings['thumb_default_show'] && !$postimage) $postimage = $tptn_settings[thumb_default]; // If no thumb found and settings permit, use default thumb
    		if ($postimage) {
    		  $postimage = apply_filters( 'tp10_postimage', $postimage, $tptn_settings[thumb_width], $tptn_settings[thumb_height] );
    		  $output .= '<img src="'.$postimage.'" alt="'.$title.'" title="'.$title.'" style="max-width:'.$tptn_settings[thumb_width].'px;max-height:'.$tptn_settings[thumb_height].'px;" border="0" class="tptn_thumb" />';
    		}
    	}
    
    	return $output;
    }

    It can be slightly optimized, but I didn't do it to keep the modifications to the code minimal.

    Hope it helps.
    Riccardo
    http://www.gurtip.it

    http://wordpress.org/extend/plugins/top-10/

  2. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Riccardo, could you please explain what exactly does this achieve?

  3. Riccardo
    Member
    Posted 1 year ago #

    In the settings for the plugin you can decide the size of the picture, but you then use the image from the post directly.
    In my specific example, I use a library to resize the image on the fly so that it doesn't have to happen in the browser. So, in my case, the filter looks like this:

    function scale_tp10_thumbs($postimage, $width, $height) {
      $new_pi = get_bloginfo("template_directory")."/img.php?src=$postimage&w=$width&h=$height";
      return $new_pi;
    }
    add_filter('tp10_postimage', 'scale_tp10_thumbs', 10, 3);

    I couldn't figure out another way to deliver the image in the right size.

    I guess another a bit more invasive way (for me, for you it might actually be a better solution) is to let the user decide which image size to use.

    Riccardo
    http://www.gurtip.it

  4. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    I think it is clearer now. If I understand it correctly, you're giving the user the freedom to run a filter on the post image. In your case you have the img.php script which resizes the same?

  5. Riccardo
    Member
    Posted 1 year ago #

    That's right, to the filter I am passing the link to the image and the size it will be visualized on the browser.
    I use the filter to pre-process the image to the expected size, so that the resource sent to the user is smaller and there is no client-side resizing of the image.

    Riccardo
    http://www.gurtip.it

  6. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Riccardo, I might be able to implement this using the inbuilt timthumb which might give better results to all users.

    Thanks for the code. I'll run some tests to see if I get the desired result

  7. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Riccardo, I've implemented the code in version 1.9.

  8. Riccardo
    Member
    Posted 1 year ago #

    Cool, glad you found it useful.

    Riccardo

  9. Ajay
    Member
    Plugin Author

    Posted 1 year ago #

    Indeed. I've used it to resize the thumbnails using timthumb.

    You shouldn't need to use your code above anymore to resize. If you need to you should use remove_filter('tp10_postimage', 'ald_scale_thumbs') before you apply your code with the add_filter

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.