Relative Site URL on Content Save

Another post for WordPress admins, building on an earlier WordPress Tip: Bloginfo as a Shortcode

Having a shortcode is great, but why not automate the insertion of the shortcode into the content box? That’s what I’ve done here.

(To use this code, add it to your theme’s functions.php file)

function bsdrelative_content_replace_url($content) {  
  return str_replace(get_bloginfo('url'),'[url]',$content);
}
add_filter('content_save_pre','bsdrelative_content_replace_url');
 
function bsdrelative_url_shortcode($atts, $content = null) {
  return get_bloginfo('url'); 
}
add_shortcode("url", "bsdrelative_url_shortcode");

There’s a reason I’m using a shortcode rather just replacing http://site.com/the/link with /the/link and it’s that the latter assumes that the site is installed at the DocumentRoot of the webserver. Not a safe assumption. Often times, I’m developing a new website for a client under a subfolder such as a http://clientsite.com/staging or http://clientsite.com/dev while the current to-be-replaced site remains at the root level.

I’ve also made this available as a WordPress Plugin

7 thoughts on “Relative Site URL on Content Save

  1. ok, I tried the plugin on a temp url
    There are 2 problems:
    1. temp url gets encoded in posts like
    http://someupaddress/%7Eaccountname
    and plugin can’t match that %7E with ~

    2. it breaks visual representation of image in tinyMCE’s visual tab and in image edit popup

    so I removed plugin, primarily of the 2. above, I’m better of using wp migrate db or wp migrate plugins

  2. Hi Chris,

    Thanks for writing the WordPress plugin. For the most part, it saved a lot of headache when migrating from the local development to the live site. Have you encountered any issues with the plugin adding an instance of “[url]/wp-admin/wp-content” to “[url]/wp-content” when updating a page or post using older versions of internet explorer or internet explorer’s compatibility mode?

    My links turn into “[url]/wp-admin/wp-content/[url]/wp-content/…” when I update a page in old versions of internet explorer for some reason. Everything works fine in modern browsers.

    This wouldn’t be a problem except that some of the site editors make updates from their work computers that only have IE.

    Thanks again,

    Mike

  3. That’s very odd. No I haven’t noticed it. Actually, I think there are a few broken image links even in this blog which I haven’t researched yet, and they use the shortcode. So maybe the issue is broader than just IE.

    This shortcode was one of my attempts to make migrating dev to production more easy. But I’ve found that even when it works, it still doesn’t do enough to address the problem. Nowadays I do a combo of searching & replacing in the database, while trying to keep the serialized data self-consistent. The task is actually greater than just replacing the domain. WordPress core and some other popular plugins write filesystem paths to the database too, and so they need to be replaced with care to match the destination server’s filesystem.

    I currently use https://github.com/Blogestudio/Fix-Serialization to help with this. Although, I’ve heard from Bill Erickson that there’s another tool out there which may work well too. Something like “Search Replace DB 2 PHP”. I’ll see if I can find it.

  4. Chris, I am working on a site that uses this plugin and I am wondering what will happen if I disable it? Will all of the url’s become inaccessible? Thanks!

  5. This is a great foundation for what I’m tiyrng to do (i think) and it works fine as long as its in my functions.php.I however have put all my shortcodes into one file for sanity’s sakefrom my child theme I call it withinclude_once locate_template(‘/inc/roots-shortcodes.php’);once its in this file, everything freezes up the site (white screen) do you have any reason why this inherently wouldn’t work?I cant figure it out. . . no errors to go by. . . many thank for a nudge in the right direction. . .

Leave a Reply

Your email address will not be published. Required fields are marked *