In my web pages I want to use relative links instead of absolute links.
However, pages do not allow php code inside them, so i cannot do to from a url in a relative manner.
How does one get relative urls inside wordpress pages so that changing domain names wouldnt affect the links?
site_url() when used by itself will return the absolute path to your blog. But, if you add an argument to it, as per my example above, it will prepend the absolute path to your path. Just make sure your URL doesn’t contain a leading slash (eg: /this/may/not/work).
Finally, if you happen to have your wordpress installed in your server’s root, you can use a server-relative path (this uses the leading slash to indicate starting at the server root). So if your blog is installed at
http://www.me.com/blog
then you can access your relative links safely with/blog/my_link.php
.WordPress has a built-in function for removing protocol and domain from absolute URLs,
wp_make_link_relative
, located in/wp-includes/formatting.php
:To apply this function to (e.g.) permalinks, simply add a filter, as such:
Deluxe Blog Tips shows how to apply this to various types of links, while making sure feed and sitemap links aren’t affected:
two ways to do that:
Linking Without Using Permalinks
Linking Using Permalinks
I used to use shortcode with get_bloginfo for relative paths inside actual post content. I was going to turn it into a full blow plugin but I could have sworn the folks at automatic released one which I can no longer find. Here’s is a simple example of how it work as I can’t find mine atm since I don’t use it anymore, as per Bainternet post, maybe the folks at automatic realized this too..
A simple shortcode function that will return your base url:
If your site is example.com then putting the shortcode [posty] in your content will return example.com as text.
To make actual use of this you will need to customize the function using more parameters from get_blogfino and wrap it in the necessary html tags
http://codex.wordpress.org/Function_Reference/get_bloginfo
use the default linking option in wordpress, remove the domain name and start it with
/
, for examplehttps://example.com/posts/post10
becomes
/posts/post10
This function should be put into your theme’s
functions.php
. It will make all permalinks relative.Images in content should use absolute links IF those images go out to feed readers. If images use relative links the feed readers try to load the images from their own domains.
There is a small performance hit in using relative links to images too. It doesn’t appear to make any difference with the relative permalinks.
On some servers, you may need to add base href with your site root to ensure links work as expected.
I think that there is an Absolute Relative Links plugin that will replace all the links in your pages with relative links. It works quite beautifully meaning that you don’t have to go back and fix any old links that are still absolute.
This tutorial shows you a basic way to change post permalink to relative. I also improved that method to make all links in WordPress relative. The final code is here.