Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic Demonstration of Using OOP in a WordPress Plugin
/*
* Plugin Header...
*
* Note: This particular block of code won't actually run because it doesn't have
* the details of the register_post_type and register_taxonomy functions filled in.
*/
class JR3_Books {
/**
* Fires all hooks for this plugin
*/
static function setup() {
add_action( 'init', array( __CLASS__, 'register_books_cpt' ) );
add_action( 'init', array( __CLASS__, 'register_genres_taxonomy' ) );
}
/**
* Basic setup of a Custom Post Type
* @see http://codex.wordpress.org/Function_Reference/register_post_type
*/
static function register_books_cpt() {
register_post_type( 'jr3_books', array(...) );
}
/**
* Register Custom Taxonomy for this Custom Post Type
* @see http://codex.wordpress.org/Function_Reference/register_taxonomy
*/
static function register_genres_taxonomy() {
register_taxonomy( 'jr3_genres', 'jr3_books', array(...) );
}
}
//Load the plugin by firing the setup() method.
add_action( 'plugins_loaded', array( 'JR3_Books', 'setup' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment