Skip to content

Instantly share code, notes, and snippets.

@carlodaniele
Last active March 16, 2021 10:34
Show Gist options
  • Save carlodaniele/865206c7c98adce0232b8d0714ef21aa to your computer and use it in GitHub Desktop.
Save carlodaniele/865206c7c98adce0232b8d0714ef21aa to your computer and use it in GitHub Desktop.
This is an example plugin showing how to register custom post types and custom taxonomies
<?php
/**
* @package frammenti
* @version 1.0
*
* Plugin Name: Frammenti
* Plugin URI: http://wordpress.org/extend/plugins/#
* Description: This is a development plugin
* Author: Carlo Daniele
* Version: 1.0
* Text Domain: frammenti
* Domain Path: /languages/
* Author URI: https://frammentidicodice.com/
*/
function frammenti_plugin_init() {
$labels = array(
'name' => _x( 'Knowledgebase', 'post type general name', 'frammenti' ),
'singular_name' => _x( 'KB article', 'post type singular name', 'frammenti' ),
'menu_name' => _x( 'Knowledgebase', 'admin menu', 'frammenti' ),
'name_admin_bar' => __( 'KB article', 'frammenti' ),
'add_new' => __( 'Add New KB', 'frammenti' ),
'add_new_item' => __( 'Add New KB', 'frammenti' ),
'new_item' => __( 'New KB', 'frammenti' ),
'edit_item' => __( 'Edit KB', 'frammenti' ),
'view_item' => __( 'View KB', 'frammenti' ),
'all_items' => __( 'All KB articles', 'frammenti' ),
'search_items' => __( 'Search KB articles', 'frammenti' ),
'parent_item_colon' => __( 'Parent KB:', 'frammenti' ),
'not_found' => __( 'No KB found.', 'frammenti' ),
'not_found_in_trash' => __( 'No KB found in Trash.', 'frammenti' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'frammenti' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'kb' ),
'show_in_rest' => true,
'capability_type' => 'post',
'taxonomies' => array( 'section', 'topic' ),
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'revisions' )
);
register_post_type( 'knowledgebase', $args );
$section_labels = array(
'name' => __( 'Sections', 'frammenti' ),
'singular_name' => __( 'Section', 'frammenti' ),
'search_items' => __( 'Search Sections', 'frammenti' ),
'all_items' => __( 'All Sections', 'frammenti' ),
'parent_item' => __( 'Parent Section', 'frammenti' ),
'parent_item_colon' => __( 'Parent Section:', 'frammenti' ),
'edit_item' => __( 'Edit Section', 'frammenti' ),
'update_item' => __( 'Update Section', 'frammenti' ),
'add_new_item' => __( 'Add New Section', 'frammenti' ),
'new_item_name' => __( 'New Section Name', 'frammenti' ),
'menu_name' => __( 'Sections', 'frammenti' )
);
$section_args = array(
'hierarchical' => true,
'labels' => $section_labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => __( 'section', 'frammenti' ),
'show_in_rest' => true,
'rewrite' => array( 'slug' => __( 'section', 'frammenti' ) ),
);
register_taxonomy( 'section', 'knowledgebase', $section_args );
$topic_labels = array(
'name' => __( 'Topics', 'frammenti' ),
'singular_name' => __( 'Topic', 'frammenti' ),
'search_items' => __( 'Search Topics', 'frammenti' ),
'all_items' => __( 'All Topics', 'frammenti' ),
'edit_item' => __( 'Edit Topic', 'frammenti' ),
'update_item' => __( 'Update Topic', 'frammenti' ),
'add_new_item' => __( 'Add New Topic', 'frammenti' ),
'new_item_name' => __( 'New Topic Name', 'frammenti' ),
'menu_name' => __( 'Topics', 'frammenti' )
);
$topic_args = array(
'hierarchical' => false,
'labels' => $topic_labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => __( 'topic', 'frammenti' ),
'show_in_rest' => true,
'rewrite' => array( 'slug' => __( 'topic', 'frammenti' ) ),
);
register_taxonomy( 'topic', 'knowledgebase', $topic_args );
}
add_action( 'init', 'frammenti_plugin_init' );
function frammenti_load_textdomain(){
load_plugin_textdomain( 'frammenti', false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action('plugins_loaded', 'frammenti_load_textdomain');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment