Skip to content

Instantly share code, notes, and snippets.

@johnregan3
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnregan3/434a511bbab95fdd33e1 to your computer and use it in GitHub Desktop.
Save johnregan3/434a511bbab95fdd33e1 to your computer and use it in GitHub Desktop.
Demonsrtation of OOP in a WordPress Custom Post Type Plugin
<?php
/*
* Plugin Name: OOP Custom Post Type Plugin
* Plugin URI: https://gist.github.com/johnregan3/434a511bbab95fdd33e1
* Description: Simple demonstration of OOP in a plugin
* Author: johnregan3
* Author URI: http://johnregan3.wordpress.com
* Version: 0.1
* Text Domain: jr3-oop-plugin
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* @package WordPress
* @subpackage JR3 OOP Plugin
*/
/**
* Contents of OOP CPT Plugin
*
* @since 0.1.0
* @access public
*/
class JR3_Books {
/**
* Fires all hooks for this plugin
*
* @static
* @action init
* @action manage_pages_custom_column
* @action restrict_manage_posts
* @filter manage_edit-jr3_Docs_columns
* @filter parse_query
* @return void
*/
static function setup() {
add_action( 'init', array( __CLASS__, 'register_books_cpt' ) );
add_action( 'init', array( __CLASS__, 'register_genres_taxonomy' ) );
/*
* Extras
*
* Just some fun stuff to do with the Post List.
* Adding/populating a custom "Genre" column, as well as a filter to sort by Genre.
*/
add_filter( 'manage_edit-jr3_books_columns', array( __CLASS__, 'register_columns' ) );
add_action( 'manage_pages_custom_column', array( __CLASS__, 'populate_column' ), 10, 2 );
add_action( 'restrict_manage_posts', array( __CLASS__, 'render_genres_filter' ) );
add_filter( 'parse_query', array( __CLASS__, 'filter_genres' ) );
}
/**
* Basic setup of a Custom Post Type
*
* @static
* @uses regsiter_post_type()
* @return void
*/
static function register_books_cpt() {
$labels = array(
'name' => __( 'Books', 'jr3-oop-plugin' ),
'singular_name' => __( 'Book', 'jr3-oop-plugin' ),
'all_items' => __( 'All Books', 'jr3-oop-plugin' ),
'add_new_item' => __( 'Add New Book', 'jr3-oop-plugin' ),
'edit_item' => __( 'Edit Book', 'jr3-oop-plugin' ),
'new_item' => __( 'New Book', 'jr3-oop-plugin' ),
'view_item' => __( 'View Book', 'jr3-oop-plugin' ),
'search_items' => __( 'Search Books', 'jr3-oop-plugin' ),
'parent_item_colon' => __( 'Parent Book', 'jr3-oop-plugin' ),
);
register_post_type( 'jr3_books',
array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'menu_position' => 21,
'hierarchical' => true,
'taxonomies' => array(
'jr3_genres',
),
'supports' => array(
'title',
'editor',
'revisions',
'page-attributes',
),
)
);
}
/**
* Register Custom Taxonomy for this Custom Post Type
*
* @static
* @return void
*/
static function register_genres_taxonomy() {
$labels = array(
'name' => __( 'Genres', 'jr3-oop-plugin' ),
'singular_name' => __( 'Genre', 'jr3-oop-plugin' ),
'all_items' => __( 'All Genres', 'jr3-oop-plugin' ),
'add_new_item' => __( 'Add New Genre', 'jr3-oop-plugin' ),
'edit_item' => __( 'Edit Genre', 'jr3-oop-plugin' ),
'new_item' => __( 'New Genre', 'jr3-oop-plugin' ),
'view_item' => __( 'View Genre', 'jr3-oop-plugin' ),
'search_items' => __( 'Search Genres', 'jr3-oop-plugin' ),
'parent_item_colon' => __( 'Parent Genre', 'jr3-oop-plugin' ),
);
register_taxonomy(
'jr3_genres',
'jr3_books',
array(
'labels' => $labels,
'show_ui' => true,
'hierarchical' => true,
)
);
}
/**
* Insert new column into Post list
*
* @static
* @param array $columns Post List columns
* @return array Updated Post List Columns
*/
static function register_columns( $columns ) {
$columns['jr3_genres'] = 'Genre';
return $columns;
}
/**
* Fill our Genres column with this post's Genre
*
* @static
* @uses get_the_term_list()
* @param array $column Post List column
* @param int $post_id Post ID
* @return void
*/
static function populate_column( $column, $post_id ) {
if ( 'jr3_genres' == $column ) {
$terms = get_the_term_list( $post_id , 'jr3_genres' , '' , ',' , '' );
if ( is_string( $terms ) ) {
echo $terms;
}
}
}
/**
* Creates a "Genres" (Taxonomy) dropdown above the Post list
*
* @static
* @uses get_current_screen()
* @uses wp_dropdown_categories()
* @global $wp_query
* @return void
*/
static function render_genres_filter() {
$screen = get_current_screen();
if ( $screen->post_type == 'jr3_books' ) {
global $wp_query;
$selected = ( isset( $wp_query->query['jr3_genres'] ) ? $wp_query->query['jr3_genres'] : '' );
$args = array(
'show_option_all' => 'All Genres',
'taxonomy' => 'jr3_genres',
'name' => 'jr3_genres',
'orderby' => 'name',
'selected' => $selected,
'hierarchical' => false,
'depth' => 3,
'show_count' => false,
'hide_empty' => true,
);
wp_dropdown_categories( $args );
}
}
/**
* Performs the project taxonomy filtering
*
* @static
* @param obj $query WP_Query object
* @return void
*/
static function filter_genres( $query ) {
$query_vars = &$query->query_vars;
if ( isset( $query_vars['jr3_genres'] ) && is_numeric( $query_vars['jr3_genres'] ) ) {
$term = get_term_by( 'id', $query_vars['jr3_genres'], 'jr3_genres' );
if ( $term ) {
$query_vars['jr3_genres'] = $term->slug;
}
}
}
}
//This loads the plugin by firing the setup() method (or "function").
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