Guest User

Untitled

a guest
Mar 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Class CustomPage_Frontend
  5. */
  6. class CustomPage_Frontend {
  7.  
  8. private static $instance = null;
  9.  
  10. public $path = 'custompage';
  11.  
  12. /**
  13. * Just to be able to call it statically.
  14. */
  15. public static function init()
  16. {
  17. if (null == self::$instance) {
  18. self::$instance = new self;
  19. }
  20.  
  21. return self::$instance;
  22. }
  23.  
  24. /**
  25. * Boot this thing up
  26. */
  27. public function __construct()
  28. {
  29.  
  30. add_action('init', array($this, 'rewrite_rules'));
  31. add_action('template_redirect', array($this, 'template_redirect'));
  32.  
  33. add_filter( 'query_vars', array($this, 'query_vars'));
  34. }
  35.  
  36. /**
  37. * Rewrites
  38. * @return Void
  39. */
  40. public function rewrite_rules()
  41. {
  42. add_rewrite_rule(
  43. '^'.$this->path.'/?$',
  44. 'index.php?'.$this->path.'=yes',
  45. 'top'
  46. );
  47. }
  48.  
  49. /**
  50. * Query vars
  51. * @param Array $vars
  52. * @return Void
  53. */
  54. public function query_vars( $vars )
  55. {
  56. $vars[] = $this->path;
  57. return $vars;
  58. }
  59.  
  60.  
  61. /**
  62. * Templates redirects
  63. * @return Void
  64. */
  65. public function template_redirect()
  66. {
  67.  
  68. if (get_query_var($this->path) == 'yes') {
  69. add_filter( 'template_include', function() {
  70.  
  71. get_template_part('templates/custom-page');
  72. });
  73. }
  74. }
  75.  
  76.  
  77.  
  78. }
  79.  
  80. CustomPage_Frontend::init();
Add Comment
Please, Sign In to add comment