Every time im about to put my local project into staging/production environment im beeing hardly reminded that WordPress saves all the urls as absolute.So i need to rewrite all the urls with a SQL-query, or use something like Search-and-replace script.
But why is WordPress doing this?
What is the benefits?
Is it a must for WordPress to work correctly?
There are themes like Roots where there are functions like roots_root_relative_url
using preg_replace
to remove the full url.
Wouldn’t it be better to only have relative urls, what is the downsides of that?
The benefits of Relative urls in the database can be:
Easy migration from local to stage/production on all links, attachments and so on.
Cleaner, shorter urls in html.
But, Because WordPress doing absolute urls i sure that its good, im only want to now why and why not.
If there were only relative URLs, what would they be relative to? A post can be viewed in several different contexts, including a feed, and a WordPress install can move into a different directory relative to root. There are issues with doing it either way, absolute URLs just give you a more concrete starting point from which to make changes.
Storing absolute URLs to your own domain makes the data structure less modular and favors the loss of long-term data integrity.
During frontend output relative stored paths could still be processed to absolute ones by simply adding WP_SITEURL to it if needed.
This way transferring a database to a different Server/Domain could be archived by only updating the WP_SITEURL via config file. String replace on sql files to change the domain is messy and can lead to unwanted behaviours.
I guess the right answer is: there is no technical reason for this.
It’s pure legacy. It once was like this, then they had to preserve retro-compatibility, they found no other way (maybe didn’t even really look for), and kept it.
WordPress is not designed for developers.
Since this question is 7 years old, I won’t go to the effort to create a full explanation, but will link to this article from Yoast that explains why using relative URLs can be problematic.