150

I've always found it frustrating in WordPress that images, files, links, etc. are inserted into WordPress with an absolute URL instead of relative URL. A relative url is much more convenient for switching domain names, changing between http and https etc. Today I discovered that if you define WP_CONTENT_URL with a relative url then when you insert files into posts they use the relative url for the src instead of absolute url. Just what I've always wanted! But the official WordPress documentation says that you should use a full URI if you are defining WP_CONTENT_URL.

WordPress codex says:

Set WP_CONTENT_URL to the full URI of this directory (no trailing slash), e.g.

define( 'WP_CONTENT_URL', 'http://example/blog/wp-content');

Everything seems to work fine when I use a relative URL, e.g.

define( 'WP_CONTENT_URL', '/my-content-folder');

But is there some problem with using a relative URI? I'm just thinking that there must be a reason for WordPress stating that it should be defined with a full URI.

11
  • 3
    I guess the answer is to audit all uses of it in WP code and in the code of plugins you're using to make sure none of them assume it's a full URL when it isn't, but I can't think of any reason it wouldn't be OK off the top of my head.
    – Rup
    Jun 21, 2013 at 23:40
  • 2
    try this plugin wordpress.org/plugins/relative-url or read this may help you deluxeblogtips.com/2012/06/relative-urls.html
    – user2543022
    Aug 27, 2013 at 16:01
  • 5
    @Krishna: Why would we need a plugin if we can just enter relative URL in wp-config? The point of the question is to find out if using relative URL in wp-config can cause a problem. Aug 28, 2013 at 21:08
  • 58
    I cry when I have to touch wordpress. Oct 23, 2014 at 15:04
  • 13
    @TonyCronin Be happy it's not Drupal.
    – motorbaby
    Sep 21, 2016 at 18:03

6 Answers 6

96
+50

I think this is the kind of question only a core developer could/should answer. I've researched and found the core ticket #17048: URLs delivered to the browser should be root-relative. Where we can find the reasons explained by Andrew Nacin, lead core developer. He also links to this [wp-hackers] thread. On both those links, these are the key quotes on why WP doesn't use relative URLs:

Core ticket:

  • Root-relative URLs aren't really proper. /path/ might not be WordPress, it might be outside of the install. So really it's not much different than an absolute URL.

  • Any relative URLs also make it significantly more difficult to perform transformations when the install is moved. The find-replace is going to be necessary in most situations, and having an absolute URL is ironically more portable for those reasons.

  • absolute URLs are needed in numerous other places. Needing to add these in conditionally will add to processing, as well as introduce potential bugs (and incompatibilities with plugins).

[wp-hackers] thread

  • Relative to what, I'm not sure, as WordPress is often in a subdirectory, which means we'll always need to process the content to then add in the rest of the path. This introduces overhead.

  • Keep in mind that there are two types of relative URLs, with and without the leading slash. Both have caveats that make this impossible to properly implement.

  • WordPress should (and does) store absolute URLs. This requires no pre-processing of content, no overhead, no ambiguity. If you need to relocate, it is a global find-replace in the database.


And, on a personal note, more than once I've found theme and plugins bad coded that simply break when WP_CONTENT_URL is defined.
They don't know this can be set and assume that this is true: WP.URL/wp-content/WhatEver, and it's not always the case. And something will break along the way.


The plugin Relative URLs (linked in edse's Answer), applies the function wp_make_link_relative in a series of filters in the action hook template_redirect. It's quite a simple code and seems a nice option.

12
  • 20
    Good info. I always use root-relative paths so that I can deploy to dev.mysite.com -> qa.mysite.com -> www.mysite.com or even www.anothersite.com with no issues. It's unfortunate that WP wasn't designed with that in mind. Since setting WP_CONTENT_URL doesn't seem to be intended to (even though it allows) setting relative urls, the plugin seems like the best option. Is there a test case where a plugin breaks when setting WP_CONTENT_URL but works fine when using the plugin?
    – Justin
    Aug 29, 2013 at 20:53
  • 27
    Wordpress was never meant for proper websites, it was always a quick hack, written by hackers who (clearly from the quotes here) haven't thought or looked outside the box. The only way you can move a website is if it CORRECTLY uses relative and/or root relative URLs. Absolute URLs absolutely prevent it.
    – Haqa
    Jun 15, 2017 at 9:13
  • 13
    I'v created sites that can be setup to live in sub-domains, root or sub folder. Its not very hard. I feel like there reasons are basically saying "We built this a long time ago and this change would break to much so we're not fixing it."
    – Donny V.
    Feb 20, 2018 at 20:42
  • 9
    still think none of the developers reasons are valid. The hard coded url has the same issues as a relative one, it's just another really bad practice the original developers have in their 'bad habits' toolkit... there are plenty of them in wordpress which is why it's been forked numerous times to remove these issues.
    – Dawesi
    Jun 21, 2018 at 10:13
  • 7
    "If you need to relocate, it is a global find-replace in the database.". According to the documentation this would break GUUIDs, and hence the whole site. Also, note that some URLs in the database have backslash-escaped slashes. But...why the schtonk didn't the devs use a placeholder like {BASE_URL} if absolute URLs are absolutely needed? "{BASE_URL}/wp-content/" might be a tad more convenient than "http://dev.example.com/subdir/wp-content" when you switch to the (https!) prod site. "Oh, I never write PI, I always write 3.141592653589793238462643383279 - easier to find in the code". Yes.
    – Klaws
    Jul 13, 2020 at 13:02
24
<?php wp_make_link_relative( $link ) ?>

Convert full URL paths to relative paths.

Removes the http or https protocols and the domain. Keeps the path '/' at the beginning, so it isn't a true relative link, but from the web root base.

Reference: Wordpress Codex

2
  • 3
    and where do you drop that php snippet?
    – Edward
    Jul 31, 2020 at 20:38
  • 1
    The real answer. Thank you!
    – jlbang
    Jan 27, 2021 at 2:57
9

I solved it in my site making this in functions.php

add_action("template_redirect", "start_buffer");
add_action("shutdown", "end_buffer", 999);

function filter_buffer($buffer) {
    $buffer = replace_insecure_links($buffer);
    return $buffer;
}
function start_buffer(){
    ob_start("filter_buffer");
}

function end_buffer(){
    if (ob_get_length()) ob_end_flush();
}

function replace_insecure_links($str) {

   $str = str_replace ( array("http://www.yoursite.com/", "https://www.yoursite.com/") , array("/", "/"), $str);

   return apply_filters("rsssl_fixer_output", $str);

}

I took part of one plugin, cut it into pieces and make this. It replaced ALL links in my site (menus, css, scripts etc.) and everything was working.

6

I agree with Rup. I guess the main reason is to avoid confusion on relative paths. I think wordpress can work from scratch with relative paths but the problem might come when using multiple plugins, how the theme is configured etc.

I've once used this plugin for relative paths, when working on testing servers:

Root Relative URLs
Converts all URLs to root-relative URLs for hosting the same site on multiple IPs, easier production migration and better mobile device testing.

7
  • 5
    Why would a plugin even be necessary if all it takes is to configure one constant in the wp-config file? Unless it provides a solution to avoid a particular pitfall of making WP_CONTENT_URL relative and does not succumb to any pitfalls with it's own approach. What I'd like to know is if there is actually a known pitfall with making WP_CONTENT_URL relative. Jun 27, 2013 at 4:57
  • 5
    A URL starting with a forward slash /wp-content/some-file.jpg is an absolute path not a relative path. There's no possible confusion. It's the inclusion of the protocol and domain name in the absolute URL that makes WordPress unhelpful without justification. To deploy a WP site from dev to staging to live I have to run a find-and-replace script on domain names in a database dump. The trouble I think is that it's designed for editing content in production which works for blogs (which is what it's made for) but not for many commercial websites.
    – Ade
    May 25, 2018 at 18:37
  • 1
    how is dude.com/bob/jane vs /bob/jane confusing? #serious #badpractice
    – Dawesi
    Jun 21, 2018 at 10:14
  • 2
    @Ade, please do your homework on relative paths. You are incorrect about /wp-content/some-file.jpg being an absolulte path. For others that read your comment in the future, please note that a web relative path is a path that has the protocol and domain excluded from the path. Thus, /wp-content/some-file.jpg and wp-content/some-file.jpg are both relative, with the leading / in the first example holding the purpose of referencing the accessible root directory of the account holder or server.
    – Pegues
    Jul 12, 2018 at 20:07
  • 1
    @Pegues OK, I found many sources (some on SO) using the term "absolute path" as opposed to "absolute url". It's also referred to as a "Root Relative" path. While semantics and terminology are of course important the point still stands that they are fundamentally different to one another.Being relative to the root has all the consistency benefits of an absolute url. Unless there's some other reasoning I am un aware of.
    – Ade
    Jul 13, 2018 at 19:14
0

Under Settings => Media, there's an option for 'Full URL-path for files'. If you set this to the default media directory path '/wp-content/uploads' instead of blank, it will insert relative paths e.g. '/wp-content/uploads/2020/06/document.pdf'.

I'm not sure if it makes all links relative, e.g. to posts, but at least it handles media, which probably is what most people are worried about.

3
  • 1
    This option 'Full URL path to files' does not appear in Media Settings by default. Is there some setting in wp-config that activates it? Jul 1, 2020 at 21:17
  • @AidanCurran I'm pretty sure it has been there for a long time, but after some research it seems like there's some wp-config enabling this after all, because it was removed by default from WP 3.5+. It can be brought back using an old plugin like: wordpress.org/plugins/upload-url-path-enabler So, I guess one of my plugins is adding this back, even though I barely have any installed. Just using Elementor and theme Futurio with plugin Futurio Extra. I tried disabling them without any change though, so not sure :/
    – hug
    Jul 2, 2020 at 23:27
  • @AidanCurran You could add something like: update_option('upload_url_path', '/wp-content/uploads'); in your functions.php file for the theme or through some code snippet plugin though, and it should have the same effect.
    – hug
    Jul 2, 2020 at 23:35
-4

What i think you do is while you change domain names, the sql dump file that you have you can replace all instances of old domain name with new one. This is only option available as there are no plugins that will help you do this.

This is quickest way ..

2
  • 1
    Bear in mind you'll also want to replace the domain name in serialised PHP strings. This handy search and replace tool seems to be the standard way of doing things (GitHub repository here). This is used by Capistrano deployment solutions I use like capistrano-wp and seems to work very well. It's also referenced on the official Moving WordPress Codex page. Aug 26, 2013 at 7:56
  • 4
    This approach is messy and error prone. If we can avoid it by using relative URLs in wp-config isn't that so much better? The point of the question is to find out if using relative URL in wp-config can cause a problem. Aug 28, 2013 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.