wordpress permalinks and local images

it took me until now to start using permalinks. the main reason: in all my previous posts, i had included images manually with the relative URI — e.g. <img src="2007/12/01/test.jpg”>.
the permalink for this post is a virtual folder
www.thinkoholic.com/2007/12/23/wordpress-permalinks-and-local-images/
and so the above image is looked for under
www.thinkoholic.com/2007/12/23/wordpress-permalinks[…]/2007/12/01/test.jpg
it’s not there of course, so it won’t be displayed correctly.
the problem is similar for internal links such as <a href="gallery.php”>.

instead of correcting every single post manually, i just added the following lines to wordpress/wp-includes/formatting.php, line 28:

$curl = str_replace('src="', 'src="/', $curl);
$curl = str_replace('src="/http://', 'src="http://', $curl);
$curl = str_replace('href="', 'href="/', $curl);
$curl = str_replace('href="/http://', 'href="http://', $curl);

lines 1 and 3 add the necessary root-slash to every src and href, lines 2 and 4 remove them for all external links.

if your all of your images and/or internal pages are in a subfolder, change the first and third line accordingly:

$curl = str_replace('src="', 'src="/subfolder/', $curl);

[ad#center]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.