Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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.

1 Answer

0 votes
by (106k points)

The problem that you are facing can be solved as follows:-

The core issue:-

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

The solution for the issue:-

  • Often WordPress is in a subdirectory, that means we will always need to process the content to then add in the rest of the path. This introduces overhead.

  • One thing always keeps 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 stores absolute URLs and 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.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...