Skip to content

Instantly share code, notes, and snippets.

@nigelheap
Created March 11, 2018 20:50
Show Gist options
  • Save nigelheap/1d34c74c61afb88d903db9447170da6f to your computer and use it in GitHub Desktop.
Save nigelheap/1d34c74c61afb88d903db9447170da6f to your computer and use it in GitHub Desktop.
Creating a custom page in wordpress from a template part with no wordpress head(), foot() or theme
<?php
/**
* Class CustomPage_Frontend
*/
class CustomPage_Frontend {
private static $instance = null;
public $path = 'custompage';
/**
* Just to be able to call it statically.
*/
public static function init()
{
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Boot this thing up
*/
public function __construct()
{
add_action('init', array($this, 'rewrite_rules'));
add_action('template_redirect', array($this, 'template_redirect'));
add_filter( 'query_vars', array($this, 'query_vars'));
}
/**
* Rewrites
* @return Void
*/
public function rewrite_rules()
{
add_rewrite_rule(
'^'.$this->path.'/?$',
'index.php?'.$this->path.'=yes',
'top'
);
}
/**
* Query vars
* @param Array $vars
* @return Void
*/
public function query_vars( $vars )
{
$vars[] = $this->path;
return $vars;
}
/**
* Templates redirects
* @return Void
*/
public function template_redirect()
{
if (get_query_var($this->path) == 'yes') {
add_filter( 'template_include', function() {
get_template_part('templates/custom-page');
});
}
}
}
CustomPage_Frontend::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment