Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create WordPress Page that redirects to another URL

Tags:

php

wordpress

I wanted to create a new WordPress page that is actually a link to another site. The goal is to have the page show up in a list of my pages, but actually send the web user to the target URL.

For example, say I want to include a page that indicates "My Photos" but actually redirects them to Flickr.

I'm guessing one way to accomplish this is by using a custom template page with a redirect instruction in PHP, but unfortunately I am a newbie to PHP and am not familiar with the way to accomplish this...

like image 399
Dscoduc Avatar asked Nov 09 '09 02:11

Dscoduc


People also ask

How do I redirect a URL to another URL?

Click the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.

How do I redirect an old URL to a new URL in WordPress?

Go to WP-Admin Dashboard > Settings > 301 Redirects > Enter the old URL under 'Requests' and the new URL under 'Destination' > Save Changes.


2 Answers

You can accomplish this two ways, both of which need to be done through editing your template files.

The first one is just to add an html link to your navigation where ever you want it to show up.

The second (and my guess, the one you're looking for) is to create a new page template, which isn't too difficult if you have the ability to create a new .php file in your theme/template directory. Something like the below code should do:

<?php /*   Template Name: Page Redirect */   header('Location: http://www.nameofnewsite.com'); exit();  ?> 

Where the template name is whatever you want to set it too and the url in the header function is the new url you want to direct a user to. After you modify the above code to meet your needs, save it in a php file in your active theme folder to the template name. So, if you leave the name of your template "Page Redirect" name the php file page-redirect.php.

After that's been saved, log into your WordPress backend, and create a new page. You can add a title and content to the body if you'd like, but the important thing to note is that on the right hand side, there should be a drop down option for you to choose which page template to use, with default showing first. In that drop down list, there should be the name of the new template file to use. Select the new template, publish the page, and you should be golden.

Also, you can do this dynamically as well by using the Custom Fields section below the body editor. If you're interested, let me know and I can paste the code for that guy in a new response.

like image 61
Schoffelman Avatar answered Oct 06 '22 02:10

Schoffelman


I've found that these problems are often best solved at the server layer. Do you have access to an .htaccess file where you could place a redirect rule? If so:

RedirectPermanent /path/to/page http://uri.com 

This redirect will also serve a "301 Moved Permanently" response to indicate that the Flickr page (for example) is the permanent URI for the old page.

If this is not possible, you can create a custom page template for each page in question, and add the following PHP code to the top of the page template (actually, this is all you need in the template:

header('Location: http://uri.com, true, 301'); 

More information about PHP headers.

like image 22
Jason Leveille Avatar answered Oct 06 '22 02:10

Jason Leveille