Skip to content

Instantly share code, notes, and snippets.

@jbreitenbucher
Created March 7, 2012 22:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbreitenbucher/1996830 to your computer and use it in GitHub Desktop.
Save jbreitenbucher/1996830 to your computer and use it in GitHub Desktop.
WordPress: Custom Post Type Template
<?php
/**
* Post Types
*
* This file registers any custom post types
*
* @package dorman-farrell
* @author The Pedestal Group <kathy@thepedestalgroup.com>
* @copyright Copyright (c) 2012, Dorman Farrell
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/**
* Create Staff post type
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
* @author The Pedestal Group
*
*/
function tpg_create_staff_post_type() {
$labels = array(
'name' => _x('Staff', 'post type general name'),
'singular_name' => _x('Staff Member', 'post type singular name'),
'add_new' => _x('Add New', 'person'),
'add_new_item' => __('Add New Staff Member'),
'edit_item' => __('Edit Staff Member'),
'new_item' => __('New Staff Memeber'),
'all_items' => __('All Staff'),
'view_item' => __('View Staff Member'),
'search_items' => __('Search Staff'),
'not_found' => __('No staff found'),
'not_found_in_trash' => __('No staff found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Staff'
);
$args = array(
'labels' => $labels,
'description' => 'A post type for entering staff information.',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'hierarchical' => false,
'supports' => array('thumbnail'),
'rewrite' => array('slug' => 'staff'),
'has_archive' => 'staff',
);
register_post_type('staff',$args);
}
add_action( 'init', 'tpg_create_staff_post_type' );
<?php
/**
* Taxonomies
*
* This file registers any custom taxonomies
*
* @package dorman-farrell
* @author The Pedestal Group <kathy@thepedestalgroup.com>
* @copyright Copyright (c) 2012, Dorman Farrell
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/**
* Create Role Taxonomy
*
* @link http://codex.wordpress.org/Function_Reference/register_taxonomy
* @author The Pedestal Group
*
*/
function tpg_create_role_taxonomy(){
$labels = array(
'name' => _x( 'Roles', 'taxonomy general name' ),
'singular_name' => _x( 'Role', 'taxonomy singular name' ),
'search_items' => __( 'Search Roles' ),
'popular_items' => __( 'Popular Roles' ),
'all_items' => __( 'All Roles' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Role' ),
'update_item' => __( 'Update Role' ),
'add_new_item' => __( 'Add New Role' ),
'new_item_name' => __( 'New Role Name' ),
'separate_items_with_commas' => __( 'Separate roles with commas' ),
'add_or_remove_items' => __( 'Add or remove roles' ),
'choose_from_most_used' => __( 'Choose from the most used roles' ),
'menu_name' => __( 'Role' ),
);
register_taxonomy(
'role',
'staff',
array(
'hierarchical' => false,
'labels' => $labels,
'public'=>true,
'show_ui'=>true,
'query_var' => true,
'rewrite' => array( 'slug' => 'role', 'with_front' => false ),
)
);
}
add_action( 'init', 'tpg_create_role_taxonomy', 0 );
<?php
/**
* Metaboxes
*
* This file registers any custom metaboxes
*
* @package dorman-farrell
* @author The Pedestal Group <kathy@thepedestalgroup.com>
* @copyright Copyright (c) 2012, Dorman Farrell
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/**
* Create Staff Metabox
*
* @link https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
* @author The Pedestal Group
*
*/
function tpg_create_metaboxes( $meta_boxes ) {
$prefix = 'tpg_'; // start with an underscore to hide fields from custom fields list
$meta_boxes[] = array(
'id' => 'staff_info_metabox',
'title' => 'Information',
'pages' => array('staff'), // post type
'context' => 'normal',
'priority' => 'low',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'First Name',
'desc' => '',
'id' => $prefix . 'first_name_text',
'type' => 'text'
),
array(
'name' => 'Last Name',
'desc' => '',
'id' => $prefix . 'last_name_text',
'type' => 'text'
),
array(
'name' => 'Title',
'desc' => 'Position title.',
'id' => $prefix . 'title_text',
'type' => 'text'
),
array(
'name' => 'Certifications',
'desc' => 'Comma separated list of certifications.',
'id' => $prefix . 'cert_text',
'type' => 'text'
),
array(
'name' => 'Phone Number',
'desc' => 'Direct dial number.',
'id' => $prefix . 'phone_number_text',
'type' => 'text'
),
array(
'name' => 'e-mail Address',
'desc' => 'Corporate e-mail address.',
'id' => $prefix . 'email_address_text',
'type' => 'text'
),
array(
'name' => 'About Me',
'desc' => 'A short description about the employee.',
'id' => $prefix . 'about_me_wysiwyg',
'type' => 'wysiwyg',
'options' => array(
'wpautop' => true, // use wpautop?
'media_buttons' => false, // show insert/upload button(s)
'textarea_rows' => get_option('default_post_edit_rows', 10), // rows="..."
),
),
array(
'name' => 'Role',
'desc' => '',
'id' => $prefix . 'role_taxonomy_select',
'taxonomy' => 'role', //Enter Taxonomy Slug
'type' => 'taxonomy_select',
),
array(
'name' => 'Order',
'desc' => 'Used to order the list of staff.',
'id' => $prefix . 'order_text',
'type' => 'text'
),
),
);
return $meta_boxes;
}
add_filter( 'cmb_meta_boxes' , 'tpg_create_metaboxes' );
/**
* Initialize Metabox Class
* see /lib/metabox/example-functions.php for more information
*
*/
function tpg_initialize_cmb_meta_boxes() {
if ( !class_exists( 'cmb_Meta_Box' ) ) {
require_once( get_stylesheet_directory() . '/lib/metabox/init.php' );
}
}
add_action( 'init', 'tpg_initialize_cmb_meta_boxes', 9999 );
/**
* Add a Role column on the Staff admin page
*
* @param array $posts_columns
* @return array $new_posts_columns
*
* @author The Pedestal Group
*
*/
function tpg_add_roles_column_to_staff_list( $posts_columns ) {
if (!isset($posts_columns['author'])) {
$new_posts_columns = $posts_columns;
} else {
$new_posts_columns = array();
$index = 0;
foreach($posts_columns as $key => $posts_column) {
if ($key=='author') {
$new_posts_columns['role'] = null;
}
$new_posts_columns[$key] = $posts_column;
}
}
$new_posts_columns['role'] = 'Roles';
$new_posts_columns['author'] = __('Author');
return $new_posts_columns;
}
/**
* Display roles for a staff member in the Roles column
*
* @param $column_id, $post_id
* @return $roles
*
* @author The Pedestal Group
*
*/
function tpg_show_role_column_for_staff_list( $column_id,$post_id ) {
global $typenow;
if ($typenow=='staff') {
$taxonomy = 'role';
switch ($column_id) {
case 'role':
$roles = get_the_terms($post_id,$taxonomy);
if (is_array($roles)) {
foreach($roles as $key => $role) {
$edit_link = get_term_link($role,$taxonomy);
$roles[$key] = '<a href="'.$edit_link.'">' . $role->name . '</a>';
}
echo implode(' | ',$roles);
}
break;
}
}
}
add_filter( 'manage_staff_posts_columns', 'tpg_add_roles_column_to_staff_list' );
add_filter('manage_staff_posts_custom_column', 'tpg_show_role_column_for_staff_list', 10, 2);
/*
Description: Adds a taxonomy filter in the admin list page for a custom post type.
Written for: http://wordpress.stackexchange.com/posts/582/
By: Mike Schinkel - http://mikeschinkel.com/custom-workpress-plugins
*/
/**
* Setup drop down for filtering according to role.
*
* @author chodorowicz
*
*/
function tpg_restrict_staff_by_role() {
global $typenow;
$args=array( 'public' => true, '_builtin' => false );
$post_types = get_post_types($args);
if ( in_array($typenow, $post_types) ) {
$filters = get_object_taxonomies($typenow);
foreach ($filters as $tax_slug) {
$tax_obj = get_taxonomy($tax_slug);
wp_dropdown_categories(array(
'show_option_all' => __('Show All '.$tax_obj->label ),
'taxonomy' => $tax_slug,
'name' => $tax_obj->name,
'orderby' => 'term_order',
'selected' => $_GET[$tax_obj->query_var],
'hierarchical' => $tax_obj->hierarchical,
'show_count' => false,
'hide_empty' => true
)
);
}
}
}
add_action('restrict_manage_posts','tpg_restrict_staff_by_role');
/**
* Convert taxonomy ID to slug
*
* @param array $query
* @return array $var
* @author chodorowicz
*
*/
function tpg_convert_role_id_to_taxonomy_term_in_query($query) {
global $pagenow;
global $typenow;
if ($pagenow=='edit.php') {
$filters = get_object_taxonomies($typenow);
foreach ($filters as $tax_slug) {
$var = &$query->query_vars[$tax_slug];
if ( isset($var) ) {
$term = get_term_by('id',$var,$tax_slug);
$var = $term->slug;
}
}
}
}
add_filter('parse_query','tpg_convert_role_id_to_taxonomy_term_in_query');
/**
* Register the Role column as sortable
*
* @param array $columns
* @return array $columns
* @author The Pedestal Group
*
*/
function tpg_role_column_register_sortable( $columns ) {
$columns['role'] = 'role';
return $columns;
}
add_filter( 'manage_edit-staff_sortable_columns', 'tpg_role_column_register_sortable' );
/**
* Set up orderby role
*
* @param array $vars
* @return array $vars
* @author The Pedestal Group
*
*/
function tpg_role_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'role' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => 'tpg_role_taxonomy_select',
'orderby' => 'meta_value'
) );
}
return $vars;
}
add_filter( 'request', 'tpg_role_column_orderby' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment