Skip to content

Instantly share code, notes, and snippets.

@jaredkc
Created December 16, 2014 22:13
Show Gist options
  • Save jaredkc/bb683e6ab08d1cd7e8dd to your computer and use it in GitHub Desktop.
Save jaredkc/bb683e6ab08d1cd7e8dd to your computer and use it in GitHub Desktop.
Basic setup for FAQ custom post type in WordPress
<?php
/**
* Post type to manage a list of frequently asked questions.
*
* @package rho
*/
if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) { die(); }
if ( ! class_exists( 'FAQ_Post_Type' ) ) :
class FAQ_Post_Type {
function __construct() {
// Runs when the plugin is activated
register_activation_hook( __FILE__, array( &$this, 'plugin_activation' ) );
// Adds post type and taxonomies
add_action( 'init', array( &$this, 'faq_init' ) );
// Show post counts in the dashboard
add_action( 'dashboard_glance_items', array( &$this, 'add_faq_counts' ) );
// Give the menu item a unique icon
add_action( 'admin_head', array( &$this, 'faq_icon' ) );
}
/**
* Flushes rewrite rules on plugin activation to ensure posts don't 404
* http://codex.wordpress.org/Function_Reference/flush_rewrite_rules
*/
function plugin_activation() {
$this->faq_init();
flush_rewrite_rules();
}
function faq_init() {
/**
* Enable the custom post type
* http://codex.wordpress.org/Function_Reference/register_post_type
*/
$labels = array(
'name' => __( 'FAQs', 'rho' ),
'singular_name' => __( 'FAQ', 'rho' ),
'add_new' => __( 'Add New FAQ', 'rho' ),
'add_new_item' => __( 'Add New FAQ', 'rho' ),
'edit_item' => __( 'Edit FAQ', 'rho' ),
'new_item' => __( 'Add New FAQ', 'rho' ),
'view_item' => __( 'View FAQ', 'rho' ),
'search_items' => __( 'Search FAQs', 'rho' ),
'not_found' => __( 'No items found', 'rho' ),
'not_found_in_trash' => __( 'No items found in trash', 'rho' )
);
$args = array(
'labels' => $labels,
'public' => true,
'supports' => array( 'title', 'editor', 'revisions' ),
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'faq' ),
'menu_position' => 5,
'has_archive' => true
);
register_post_type( 'faq', $args );
}
/**
* Add count to "At a Glance" Dashboard Widget
*/
function add_faq_counts() {
if ( ! post_type_exists( 'faq' ) ) {
return;
}
$post_types = array( 'faq' ); // array of custom post types to add to 'At A Glance' widget
foreach ( $post_types as $pt ) :
$pt_info = get_post_type_object( $pt ); // get a specific CPT's details
$num_posts = wp_count_posts( $pt ); // retrieve number of posts associated with this CPT
$num = number_format_i18n( $num_posts->publish ); // number of published posts for this CPT
$text = _n( $pt_info->labels->singular_name, $pt_info->labels->name, intval( $num_posts->publish ) ); // singular/plural text label for CPT
echo '<li class="page-count ' . $pt_info->name . '-count"><a href="edit.php?post_type=' . $pt . '">' . $num . ' ' . $text. '</a></li>';
endforeach;
}
/**
* Displays the custom post type icon in the admin
*/
function faq_icon() { ?>
<style type="text/css" media="screen">
#adminmenu #menu-posts-faq div.wp-menu-image:before,
#dashboard_right_now .page-count.faq-count a:before {
content: "\f223";
}
</style>
<?php }
}
new FAQ_Post_Type;
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment