Skip to content

Instantly share code, notes, and snippets.

@monkishtypist
Created September 9, 2018 17:14
Show Gist options
  • Save monkishtypist/f7489dda306b3fa623325bef58bc31de to your computer and use it in GitHub Desktop.
Save monkishtypist/f7489dda306b3fa623325bef58bc31de to your computer and use it in GitHub Desktop.
FAQs custom post type for WordPress
<?php
/*
Plugin Name: FAQs Custom Post Type
Plugin URI: https://github.com/monkishtypist
Description: This `mu-plugin` class creates a new custom post type for Frequently Asked Questions.
Version: 1.0
Author: Tim Spinks @monkishtypist
Author URI: https://github.com/monkishtypist
*/
if ( ! class_exists( 'CPT_FrequentlyAskedQuestions' ) ) :
class CPT_FrequentlyAskedQuestions {
public function __construct() {
add_action( 'init', array( $this, 'faq_tax_init') );
add_action( 'init', array( $this, 'faq_post_type') );
}
/**
* Register custom taxonomy
*/
public function faq_tax_init() {
$labels = array(
'name' => _x( 'FAQ Categories', 'faq_categories_taxonomy' ),
'singular_name' => _x( 'FAQ Category', 'faq_categories_taxonomy' ),
'search_items' => __( 'Search FAQ Categories' ),
'popular_items' => __( 'Popular FAQ Categories' ),
'all_items' => __( 'All FAQ Categories' ),
'parent_item' => __( 'Parent FAQ Category' ),
'parent_item_colon' => __( 'Parent FAQ Category:' ),
'edit_item' => __( 'Edit FAQ Category' ),
'view_item' => __( 'View FAQ Category' ),
'update_item' => __( 'Update FAQ Category' ),
'add_new_item' => __( 'Add New FAQ Category' ),
'new_item_name' => __( 'New Position' ),
'separate_items_with_commas' => __( 'Separate FAQ categories with commas' ),
'add_or_remove_items' => __( 'Add or remove FAQ categories' ),
'choose_from_most_used' => __( 'Choose from the most used FAQ categories' ),
'not_found' => __( 'No FAQ categories found' ),
'no_terms' => __( 'No FAQ categories' ),
'items_list_navigation' => __( 'Items list navigation' ),
'items_list' => __( 'Items list' ),
'most_used' => __( 'Most Used' ),
'back_to_terms' => __( 'Back to FAQ categories' )
);
$rewrite = array(
'slug' => 'faq_categories',
'with_front' => false,
'hierarchical' => true
);
$args = array(
'labels' => $labels,
'description' => __( '' ),
'public' => true,
'publicly_queryable' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_admin_column' => true,
'rewrite' => $rewrite
);
register_taxonomy( 'faq-categories', 'cpt-faqs', $args );
}
/**
* Register "FAQs" post type
*/
public function faq_post_type() {
$labels = array(
'name' => _x( 'FAQs', 'Post Type General Name' ),
'singular_name' => _x( 'FAQ', 'Post Type Singular Name' ),
'menu_name' => _x( 'FAQs', 'Post Type Singular Name' ),
'name_admin_bar' => _x( 'FAQs', 'Post Type Singular Name' ),
'archives' => __( 'FAQ Archives' ),
'parent_item_colon' => __( 'Parent Item:' ),
'all_items' => __( 'All FAQs' ),
'add_new_item' => __( 'Add New FAQ' ),
'add_new' => __( 'Add New' ),
'new_item' => __( 'New FAQ' ),
'edit_item' => __( 'Edit FAQ' ),
'update_item' => __( 'Update FAQ' ),
'view_item' => __( 'View FAQ' ),
'search_items' => __( 'Search FAQs' ),
'not_found' => __( 'Not found' ),
'not_found_in_trash' => __( 'Not found in Trash' ),
'featured_image' => __( 'Featured Image' ),
'set_featured_image' => __( 'Set featured image' ),
'remove_featured_image' => __( 'Remove featured image' ),
'use_featured_image' => __( 'Use as featured image' ),
'insert_into_item' => __( 'Insert into item' ),
'uploaded_to_this_item' => __( 'Uploaded to this item' ),
'items_list' => __( 'Items list' ),
'items_list_navigation' => __( 'Items list navigation' ),
'filter_items_list' => __( 'Filter items list' )
);
$rewrite = array(
'slug' => 'faqs',
'with_front' => false,
'pages' => true,
'feeds' => false
);
$args = array(
'label' => __( 'FAQ' ),
'description' => __( '' ),
'labels' => $labels,
'supports' => array( 'title', 'editor' ),
'taxonomies' => array( 'faq-categories'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-editor-help',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
'show_admin_column' => true
);
register_post_type( 'cpt-faqs', $args );
}
}
$CPT_FrequentlyAskedQuestions = new CPT_FrequentlyAskedQuestions();
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment