Skip to content

Instantly share code, notes, and snippets.

@johnregan3
Last active December 27, 2019 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save johnregan3/5868f4349f825a9b50b9 to your computer and use it in GitHub Desktop.
Save johnregan3/5868f4349f825a9b50b9 to your computer and use it in GitHub Desktop.
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