Th3_G4m3r

template-redirect.php

Mar 2nd, 2013
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Template Name: Page Redirect
  5.  
  6. * @author Dave Stewart
  7. * @web www.davestewart.co.uk
  8.  
  9. * @name Page Redirect
  10. * @type PHP page
  11. * @desc Wordpress template that redirects the current page based on the content of the database entry it loads
  12.  
  13. * @requires Wordpress
  14. * @install Copy this file to the directory of the theme you wish to use
  15. * usage
  16. 1. Create a new Page in your Wordpress control panel
  17. 2. Enter the URL (or local path, relative to your Wordpress directory) you want to redirect to as the only page content
  18. 3. Set the Page Template to "Page Redirect"
  19. 4. Publish
  20. */
  21.  
  22. if (function_exists('have_posts') && have_posts()){
  23. while (have_posts()){
  24.  
  25. // get the post
  26. the_post();
  27.  
  28. // get content
  29. ob_start();
  30. the_content();
  31. $contents = ob_get_contents();
  32. ob_end_clean();
  33.  
  34. // correctly build the link
  35.  
  36. // grab the 'naked' link
  37. $link = strip_tags($contents);
  38. $link = preg_replace('/\s/', '', $link);
  39.  
  40. // work out
  41. if(!preg_match('%^http://%', $link)){
  42. $host = $_SERVER['HTTP_HOST'];
  43. $dir = dirname($_SERVER['PHP_SELF']);
  44. $link = "http://$host$dir/$link";
  45. }
  46.  
  47. // do the link
  48. header("Location: $link");
  49. die('');
  50. }
  51.  
  52. }
  53. ?>
Advertisement
Add Comment
Please, Sign In to add comment