How to redirect to another url in wordpress?

We can redirect website visitor to another url in wordpress using wp_redirect() function.

wp_redirect()

This function redirects the user to a specified url. To use this function edit your page template and specify the function at the top as per following  syntax:

This wordpress function takes 2 parameters :

  • $location-url (required) : url to redirect where you want to redirect visitors to. It’s required to specify. It can be internal or external link.
  • $status (optional) : status code to use. Default is 302. This parameter specifies the type of redirect. (see all http status codes at wikipedia) 

for example if you want visitors to move from an old page to a new one as well as permanent redirection to make Google to transfer your page rank & inbound link to the new page, your status code will be 301.

Note:

  1. wp_redirect() does not exit automatically and so always follow it by “exit” keyword.
  2. Always call this function before any output is sent to the browser otherwise you will likely get a “headers already sent” error.

Examples :

That’s it. Now you can easily use the safer WordPress function instead of the old php header method.

Keep continue to visit our website dev.fellowtuts.com. We will describe more methods for redirection and to get rid from the error “headers already sent” in our upcoming articles.

You Might Interested In