-
-
Save awolad/143de7b721f45c5761c07318060f969e to your computer and use it in GitHub Desktop.
An example of how to create a WordPress Custom Post type using an Object Oriented approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Use namespace to avoid conflict | |
*/ | |
namespace PostType; | |
/** | |
* Class Event | |
* @package PostType | |
* | |
* Use actual name of post type for | |
* easy readability. | |
* | |
* Potential conflicts removed by namespace | |
*/ | |
class Event { | |
/** | |
* @var string | |
* | |
* Set post type params | |
*/ | |
private $type = 'event'; | |
private $slug = 'events'; | |
private $name = 'Events'; | |
private $singular_name = 'Event'; | |
/** | |
* Register post type | |
*/ | |
public function register() { | |
$labels = array( | |
'name' => $this->name, | |
'singular_name' => $this->singular_name, | |
'add_new' => 'Add New', | |
'add_new_item' => 'Add New ' . $this->singular_name, | |
'edit_item' => 'Edit ' . $this->singular_name, | |
'new_item' => 'New ' . $this->singular_name, | |
'all_items' => 'All ' . $this->name, | |
'view_item' => 'View ' . $this->name, | |
'search_items' => 'Search ' . $this->name, | |
'not_found' => 'No ' . strtolower($this->name) . ' found', | |
'not_found_in_trash' => 'No ' . strtolower($this->name) . ' found in Trash', | |
'parent_item_colon' => '', | |
'menu_name' => $this->name | |
); | |
$args = array( | |
'labels' => $labels, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => $this->slug ), | |
'capability_type' => 'post', | |
'has_archive' => true, | |
'hierarchical' => true, | |
'menu_position' => 8, | |
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail'), | |
'yarpp_support' => true | |
); | |
register_post_type( $this->type, $args ); | |
} | |
/** | |
* @param $columns | |
* @return mixed | |
* | |
* Choose the columns you want in | |
* the admin table for this post | |
*/ | |
public function set_columns($columns) { | |
// Set/unset post type table columns here | |
return $columns; | |
} | |
/** | |
* @param $column | |
* @param $post_id | |
* | |
* Edit the contents of each column in | |
* the admin table for this post | |
*/ | |
public function edit_columns($column, $post_id) { | |
// Post type table column content code here | |
} | |
/** | |
* Event constructor. | |
* | |
* When class is instantiated | |
*/ | |
public function __construct() { | |
// Register the post type | |
add_action('init', array($this, 'register')); | |
// Admin set post columns | |
add_filter( 'manage_edit-'.$this->type.'_columns', array($this, 'set_columns'), 10, 1) ; | |
// Admin edit post columns | |
add_action( 'manage_'.$this->type.'_posts_custom_column', array($this, 'edit_columns'), 10, 2 ); | |
} | |
} | |
/** | |
* Instantiate class, creating post type | |
*/ | |
new Event(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment