Skip to content

Instantly share code, notes, and snippets.

@iagdotme
Last active December 22, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iagdotme/6422105 to your computer and use it in GitHub Desktop.
Save iagdotme/6422105 to your computer and use it in GitHub Desktop.
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