December 25 2013

A better way to get item url in WordPress

Tagged Under :

Wordpress
When linking to posts or images from your posts, there is a more versatile means of doing so. Imagine that you change themes or move to another domain. You need to go through entire posts to search and replace every link.

Maybe you will think linking the posts or images like below example:
<a href="../contact/" >Contact Us</a>
But it was bad way.

This is worse as well:
<a href="http://chevronscode.com/contact/" >Contact Us</a>

The best solution is:
<a href="[item_url]/contact/" >Contact Us</a>
Refer above example the [item_url] will always valid even if you change the themes or domain.

How to do it. you just need create a new shortcode at function.php

Copy the following code into your functions.php file.
function new_item_url() {
  return content_url();
}
add_shortcode('item_url', 'new_content_url');
The above shortcode will return your site url.

Below is other functions to retrieve specific files from other directories. You just need replace the content_url() as you need.
site_url();
admin_url();
content_url();
plugins_url();
includes_url();

Make a Comment

*

code