Skip to content

Instantly share code, notes, and snippets.

@carmichaelize
Last active August 29, 2015 14:00
Show Gist options
  • Save carmichaelize/11060367 to your computer and use it in GitHub Desktop.
Save carmichaelize/11060367 to your computer and use it in GitHub Desktop.
Wordpress Custom Post Type Wrapper
<?php
class custom_post_type {
//Custom post type name
public $post_type_name = 'name';
//Custom metabox key
public $key = $this->post_type_name = 'key';
//Custom metabox options
public $options = array(
'post_types' => array( $this->post_type_name ),
'title' => 'Metabox Title',
'context' => 'side', //normal, advanced, side
'priority' => 'default' //default, core, high, low
);
public post_type_options = array(
'labels' => array(
'name' => __( 'Item' ),
'singular_name' => __( 'Items' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Item' ),
'edit_item' => __( 'Edit Item' ),
'new_item' => __( 'New Item' ),
'all_items' => __( 'All Items' ),
'view_item' => __( 'View Item' ),
'search_items' => __( 'Search Items' ),
'not_found' => __( 'No items found.' ),
'not_found_in_trash' => __( 'No items found in the trash.' ),
'parent_item_colon' => '',
'menu_name' => 'Items'
),
'description' => 'Holds our testimonials and testimonial specific data',
'public' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-admin-comments', //http://melchoyce.github.io/dashicons/
'supports' => array( 'title', 'editor' ), // title, editor, thumbnail, page-attributes excerpt, comments
'has_archive' => true,
'rewrite' => array(
'slug' => 'items',
'with_front' => true
)
);
public function post_type_setup() {
register_post_type( $this->post_type_name, $this->post_type_options );
}
//Custom Taxonomies
/*
public post_taxonomy_options = array(
'labels' => array(
'name' => __( 'Taxonomys' ),
'singular_name' => __( 'Taxonomy' ),
'search_items' => __( 'Search Taxonomys' ),
'all_items' => __( 'All Taxonomys' ),
'parent_item' => __( 'Parent Taxonomy' ),
'parent_item_colon' => __( 'Parent Taxonomy' ),
'edit_item' => __( 'Edit Taxonomy' ),
'update_item' => __( 'Update Taxonomy' ),
'add_new_item' => __( 'Add New Taxonomy' ),
'new_item_name' => __( 'New Taxonomy Name' ),
'menu_name' => __( 'Taxonomys' ),
),
'hierarchical' => false, // Hierarchical taxonomy (like categories)
'rewrite' => array(
'slug' => 'locations',
'with_front' => false,
'hierarchical' => true
)
);
public function post_taxonomy_setup(){
register_taxonomy( 'taxonomy', $this->post_type_name, $this->post_taxonomy_options );
}
*/
//Custom Metabox
public function custom_meta_add() {
foreach( $this->options->post_types as $post_type ){
//$key, $title, $callback, $post_type, $context, $priority, $callback_args
add_meta_box(
$this->key,
esc_html__( $this->options->title, 'example' ),
array(&$this, 'custom_meta_render' ),
$post_type,
$this->options->context,
$this->options->priority,
$callback_args = null
);
}
}
public function custom_meta_render( $object, $box ){
wp_nonce_field( basename( __FILE__ ), $this->key.'_nonce' );
$data = get_post_meta( $object->ID, $this->key, true );
//Render metabox HTML
//<input type="text" class="widefat" value="" />
}
public function custom_meta_save( $post_id, $post = false ){
//Verify the nonce before proceeding.
if ( !isset( $_POST[$this->key.'_nonce'] ) || !wp_verify_nonce( $_POST[$this->key.'_nonce'], basename( __FILE__ ) ) ){
return $post_id;
}
//Get the posted data and sanitize it for use as an HTML class.
$new_meta_value = ( isset( $_POST[$this->key] ) ? $_POST[$this->key] : '' );
//Get previously saved data
$meta_value = get_post_meta( $post_id, $this->key, true );
//Add, update and delete data
if( $new_meta_value && '' == $meta_value ){
add_post_meta( $post_id, $this->key, $new_meta_value, true );
} elseif ( $new_meta_value && $new_meta_value != $meta_value ){
update_post_meta( $post_id, $key, $new_meta_value );
} elseif ( '' == $new_meta_value && $meta_value ){
delete_post_meta( $post_id, $key, $meta_value );
}
}
public function custom_meta_setup() {
//Add metabox
add_action( 'add_meta_boxes', array(&$this, 'custom_meta_add' ));
//Save metabox
add_action( 'save_post', array(&$this, 'custom_meta_save'));
}
public function __construct(){
//Add custom post types
add_action( 'init', array( &$this, 'post_type_setup' ) );
//Add custom taxonomy to post type
//add_action( 'init', array( &$this, 'post_taxonomy_setup' ), 0 );
//Create options object
$this->options = (object)$this->options;
//Add custom metabox
add_action( 'init', array( &$this, 'custom_meta_setup' ) );
}
}
new custom_post_type();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment