Portable Wordpress

25 Aug 2017 in Wordpress

Many times I've seen developers on wordpress struggling when transferring the installation from a development/staging environment to production.

One tip is to use the WP_HOME and WP_SITEURL constants as defined in wp-config.php when creating static links.

So instead of using something like this:

<a href="http://example.com/a-link">

Use

<a href="<?= WP_SITEURL ?>/a-link">

Of course this applies only to PHP files.

For css you can take advantage of HTML <base> element somewhere in your html head like this:

<base href="<?= WP_SITEURL ?>">

Any resource, like background images, referenced in css will be relative to the url above and you don't have to mess with absolute urls any more.

Beware that the usage of an anchor tag is resolved by using the base url as reference and triggers an http request to the base url. To circumvent that, you may use absolute resources in css at the cost of not developing in subfolders anymore.