Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

scribu commented Jun 7, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.