December 25 2013

A better way to get item url in WordPress

Tagged Under :

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="" >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.

Make a Comment