Solution to WordPress 2.7.1 Gallery Code Problem

Friday, March 13, 2009 @ 5:30 pm by Sofie Hofmann

I tried to find a solution to the WordPress 2.7.1 gallery code problem. I checked some templates first but I was not able to identify the right one. So I searched the web and I found the solution from this blog entry, Problem with WordPress 2.7.1 and images.

I was looking into several templates like link-template.php and post-template.php but did not think about the post.php file. With his solution, I was able to solve my problem that I told you in my previous blog entry.

This was the PHP function wp_get_attachment_url at WordPress 2.7.

function wp_get_attachment_url( $post_id = 0 ) {
	$post_id = (int) $post_id;
	if ( !$post =& get_post( $post_id ) )
		return false;

	$url = '';
	if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { //Get attached file
		if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory
			if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
				$url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
		}
	}

	if ( empty($url) ) //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recomended to rely upon this.
		$url = get_the_guid( $post->ID );

	if ( 'attachment' != $post->post_type || empty($url) )
		return false;

	return apply_filters( 'wp_get_attachment_url', $url, $post->ID );
}

If you read the blog entry where I found the solution, you will see there the present PHP function wp_get_attachment_url at WordPress 2.7.1.

I just commented (using // before each line) the following four lines which was added or included or inserted at the PHP function wp_get_attachment_url at the post.php when WordPress 2.7.1 was released. You will find post.php file under wp-includes folder.

elseif ( false !== strpos($file, 'wp-content/uploads') )
				$url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
			else
				$url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir.

That’s it. That’s the solution to my gallery code problem. Thank you, emmanuel-georjon.

This entry was posted on Friday, March 13, 2009 at 5:30 pm and is filed under PHP, WordPress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Related Posts

2 Comments to “Solution to WordPress 2.7.1 Gallery Code Problem”

  1. [...] I just commented the particular codes which I already mentioned in my blog entry Solution to WordPress 2.7.1 Gallery Code Problem. [...]

  2. [...] have a problem with WordPress Image Gallery and you need a solution, kindly read this blog entry, Solution to WordPress 2.7.1 Gallery Code Problem. I have been having this problem with WordPress Image Gallery since upgrading to WordPress 2.7.1 [...]

Leave a Reply

Commenting is under moderation.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

 
 
 
 
 
 

Who is behind Sehdi.com?

I am Sofie Hofmann and I have been designing websites since 1999. My background is in training, sales and marketing management before venturing into web design and development but merely as a hobby.
Read more...

Make money with Text Link Ads
Text Link Ads