{"id":1000,"date":"2010-02-09T16:47:30","date_gmt":"2010-02-09T15:47:30","guid":{"rendered":"http:\/\/www.maratz.com\/blog\/?p=1000"},"modified":"2012-12-23T16:45:30","modified_gmt":"2012-12-23T15:45:30","slug":"simple-page-or-section-redirect-in-wordpress","status":"publish","type":"post","link":"https:\/\/www.maratz.com\/blog\/archives\/2010\/02\/09\/simple-page-or-section-redirect-in-wordpress\/","title":{"rendered":"Simple page or section redirect in WordPress"},"content":{"rendered":"<p>Recently we had a chance to re-code the old client site: <a href=\"http:\/\/www.graniti-susec.hr\/\">Graniti-Su\u0161ec<\/a>. The site was first developed back in 2003. and was in fact the first commercial (non-personal) site done with web standards in Croatia.<\/p>\n<p>At that time I was much more involved with ASP 2.0 and merely heard about <abbr>PHP<\/abbr>. But since then \u2014 my focus shifted far, far away from ASP, and we decided not to fix the ancient codebase, but to move the backend to WordPress.<\/p>\n<p>With the migration, we had to take care about the new <abbr>URL<\/abbr> scheme. The language part of the previous scheme was a full-length language name and therefore clumsy. The transition had to be seamless, and we had to make sure not to cut-off visitors landing to the website from Google.<\/p>\n<p>The solution was quite simple. Each page was organized as a child page of one of two parent pages: <code>\/hr\/<\/code> and <code>\/en\/<\/code>. We created two additional first-level pages with slugs corresponding to the old language <abbr>URL<\/abbr> parts \u2014 <code>\/hrvatska\/<\/code> and <code>\/english\/<\/code> \u2014 and set a template for both pages to &#8220;Page Redirect&#8221;. This is how it\u2019s done step by step:<!--more--><\/p>\n<h2>Step 1: Create a page template<\/h2>\n<p>In your WordPress theme folder create a new blank file: <code>page-redirect.php<\/code>, paste in the code below and save it. Make sure that there\u2019s no space left before <code>&#60;?php<\/code> or after <code>?><\/code> or it might throw an error.<\/p>\n<p><code><\/p>\n<pre>&#60;?php \r\n\/* \r\nTemplate Name: Page Redirect \r\n*\/ \r\n    header('HTTP\/1.1 301 Moved Permanently'); \r\n\r\n    $redirect_url = get_post_meta($post->ID, 'redirect_url', true); \r\n\r\n    if (!empty($redirect_url)) { \r\n        header('Location: ' . $redirect_url); \r\n    } else { \r\n        header('Location: \/'); \r\n    } \r\n?><\/pre>\n<p><\/code><\/p>\n<h2>Step 2: Add a new page<\/h2>\n<p>Create a new page in WordPress (Pages \u00bb Add New), and paste the old <abbr>URL<\/abbr> into <em>Permalink<\/em> field \u2014 right below the title field.<\/p>\n<h2>Step 3: Add a custom field<\/h2>\n<p>In the Custom Fields box, add a new custom field, named <code>redirect_url<\/code> and set the value to match the new <abbr>URL<\/abbr>.<\/p>\n<h2>Step 4: Choose a page template<\/h2>\n<p>In the attribute box in WordPress (usually on the right hand side), there is the Templates drop-down. Select the &#8220;Page Redirect&#8221; option and hit Publish.<\/p>\n<p>Type the old <abbr>URL<\/abbr> in the browser address bar, and it should be instantly redirected to the new <abbr>URL<\/abbr>. WordPress will also automagically redirect all child pages, so in our example <code>\/english\/marble\/<\/code> redirects to <code>\/en\/marble\/<\/code>.<\/p>\n<p>I hope it helps, let me know if you have a different approach to a similar problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently we had a chance to re-code the old client site: Graniti-Su\u0161ec. The site was first developed back in 2003. and was in fact the first commercial (non-personal) site done with web standards in Croatia. At that time I was much more involved with ASP 2.0 and merely heard about PHP. But since then \u2014 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,21],"tags":[],"_links":{"self":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/1000"}],"collection":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/comments?post=1000"}],"version-history":[{"count":0,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/1000\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/media?parent=1000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/categories?post=1000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/tags?post=1000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}