Skip to content

Instantly share code, notes, and snippets.

@scribu
Created June 7, 2012 17:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scribu/2890140 to your computer and use it in GitHub Desktop.
Save scribu/2890140 to your computer and use it in GitHub Desktop.
APP_View_Generic
<?php
/**
* Helper class for using the generic.php template from a plugin.
*
* Optional methods:
* - init() - for registering post types, taxonomies, rewrite rules etc.
* - before_load_template() - for processing forms, enqueueing scripts etc
*/
abstract class APP_View_Generic {
/**
* Test if this controller should handle the current page.
*
* Use is_*() conditional tags and get_query_var()
*
* @return bool
*/
abstract function condition();
/**
* Use this to actually display something within generic.php
*
* Can be another call to locate_template()
*/
abstract function load_template();
function __construct() {
if ( method_exists( $this, 'init' ) )
add_action( 'init', array( $this, 'init' ) );
add_action( 'template_redirect', array( $this, '_template_redirect' ) );
}
final function _template_redirect() {
if ( !$this->condition() )
return;
if ( method_exists( $this, 'before_load_template' ) )
$this->before_load_template();
add_action( 'generic_content', array( $this, 'load_template' ) );
locate_template( 'generic.php', true );
die;
}
}
<?php
class My_Generic_View extends APP_View_Generic {
function init() {
global $wp;
$wp->add_query_var( 'my_plugin_query_var' );
}
function condition() {
return get_query_var( 'my_plugin_query_var' );
}
function load_template() {
echo '<p>In the generic.php template.</p>';
}
}
new My_Generic_View;
@scribu
Copy link
Author

scribu commented Jun 7, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment