Skip to content
Article

WordPress Plugin – ABT Relative URLs

Here’s a WordPress plugin that will help you transfer your site across domains by stripping out the default “absolute url” from post content with a relative “/”; note that this works as you create content, so it should be the first plugin you install!

Download ABT Relative Urls  Download from WordPress

As taken from the readme file:


Description

By default, WP inserts absolute urls into post content; this includes the protocol and domain, which is based on the home Admin setting. This plugin replace all self-referencing (domain) links with relative paths “/” instead. Works when inserting images into posts, and on the actual save_post action it scrubs the contentexcerpt, and post_meta fields.

Especially helpful when developing on a DEV site with the intention of transfering to a LIVE domain. Please note that you should use this plugin before you start adding content, or you’ll have to resave everything later.

Includes code based on Relative Image URLs plugin, which strips domain when inserting images from the Media Library. A similar idea to Absolute to Relative URLs, but works automatically, and handles meta fields.


Installation

  1. Unzip, upload plugin folder to your plugins directory (/content/plugins/)
  2. Activate plugin
  3. Create content – view HTML source to ensure that domains have been stripped from content.

Frequently Asked Questions

How does it work?

First, it determines the absolute URL from the admin settings (home key, via home_url()), as this is what WordPress uses when hardcoding links.

On save_post action, it examines both the post_content and post_excerpt submissions and strips the current domain/protocol from:

  1. href attributes
  2. src attributes
  3. all other instances of the domain

It then retrieves all of the postmeta, scans through the array, and removes the domain from any values. Since it’s a direct dump, it maybe_unserializes each value before recursively scrubbing the content.

Can I use the absolute URL?

Yes with protected shortcode:

[abt_absolute_url trailing="/suffix/"] 

where the attribute trailing is optional, and would append whatever is given to the absolute url. Really it’s just provided as a “just-in-case”, as you could write [abt_absolute_url]/suffix/ just the same.

Can I change what’s replaced?

By default, the following:

array( 'src="' . $absolute_path_prefix , 'href="' . $absolute_path_prefix , esc_attr( $absolute_path_prefix ) ) 

are replaced with

array( 'src="' . $relative_path_prefix , 'href="' . $relative_path_prefix , esc_attr( $relative_path_prefix ) ) 

Two hooks are provided to alter these defaults:

  • abt_relative_urls_get_search_for: the first “search” array
  • abt_relative_urls_get_replace_with: the second “replace” array

The reason it looks for the src, then href, then the actual attribute has to do with compatibility with the included relative image url plugin.

Developers

Check out our other developer-centric plugin, WP-Dev-Library. Suggestions/improvements welcome!

The Atlantic BT Manifesto

The Ultimate Guide To Planning A Complex Web Project