Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert absolute URLs in blog and page content to site relative ones. The conversion happens when you save or publish content. For example <img src="http://www.yoursite.com/image.gif" /> will be converted to <img src="/image.gif" />
<?php
/* ---------------------------------------------------------------------------------- */
/* Convert absolute URLs in content to site relative ones
Inspired by http://thisismyurl.com/6166/replace-wordpress-static-urls-dynamic-urls/
*/
function sp_clean_static_url($content) {
$thisURL = get_bloginfo('url');
$stuff = str_replace(' src=\"'.$thisURL, ' src=\"', $content );
$stuff = str_replace(' href=\"'.$thisURL, ' href=\"', $stuff );
return $stuff;
}
add_filter('content_save_pre','sp_clean_static_url','99');
/* ---------------------------------------------------------------------------------- */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.