Back to Top

How to use an external URL in WordPress?


After a long I get time to post because of very busy schedule.Today I arranged some time and come with something new. In this article, I am going to explain how you can use external URL in WordPress and redirect to that URL when clicking on post/link.

Recently, I was working on one theme and in that I am importing posts from other sites and want to redirect to actual /origin site if click on title or link found in google.

So, Let’s understand how I have done it. I have stored external URL/link as guid into the post in WordPress and permalink will be a URL of our site.So, we need to rewrite and redirect to URL and need to change the permalink.

Well, following filter hook is used to replace permalink with guid, if post’s guid is different than the site’s base URL. Here, I can get Host URL by parse_url function.

Next, action hook is used for redirecting template means if permalink of the own site found in somewhere or in the search engine, We also need to redirect to external/ original URL.Here, I have used the same condition like above just changes code of redirection.

That’s it. Place above code into your theme’s function.php file and run the code into browser :).

Recommended Read:

WordPress Action Hook
Auto-tagging in WordPress
To get Last auto increment id in WordPress

Hope this helps someone else out.As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (1)

  1. faten sultan says:

    thanks for the code
    i am searching on the opposite side
    i have working non-wordpress site
    and i am transferring now to wordpress and i want my old links url (5400 link) to be the same as old
    i know solution is 301 redirection but actually i don’t think i can do that , as my url’s are complected
    is there any other way to do that???

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

The Readers’ Poll – November 2012

Posted on 6 years ago


New Features in WordPress 3.5

Posted on 6 years ago


Getting started with Laravel

Posted on 1 year ago


How to create MySQL User Defined Functions

Posted on 6 years ago


How to use Flickr API with PHP

Posted on 6 years ago