Page Redirect Template

A handy little page template for making a page redirect to any url. To use save the code as redirect.php, upload to your theme directory, and then when making a page select “redirect” as your page template. Then just enter ONLY the url you want to redirect to in the body copy for that page.

<?php
/*
Template Name: Redirect
*/

if (have_posts()) {
  the_post();
  $pattern = '@(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)@';
  preg_match($pattern, get_the_excerpt(), $matches);
  if ($matches[0]) header('Location: '.$matches[0]);
  else echo 'Enter a URL into your page body text.';
}
?>

Revisions

There are no revisions for this post.

Tags: ,

No comments yet.

Leave a Reply