Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A tiny little article plugin to demo Custom Post Types code to almost match Extended CPTs (no sortable admin columns or filters, and no "At a Glance" entry.
<?php
/**
* @link https://deliciousbrains.com/blog/
* @since 1.0
* @package Pixie_Article
*
* @wordpress-plugin
* Plugin Name: Pixie Article
* Plugin URI: https://deliciousbrains.com/blog/
* Description: A tiny little article plugin to demo Custom Post Types.
* Version: 1.0
* Author: ianmjones
* Author URI: https://profiles.wordpress.org/ianmjones
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: pixie-article
* Domain Path: /languages
* Network: False
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
function pixie_article_activation() {
pixie_article_loaded();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'pixie_article_activation' );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
function pixie_article_loaded() {
load_plugin_textdomain(
'pixie-article',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
$labels = array(
'name' => __( 'Pixie Articles', 'pixie-article' ),
'singular_name' => __( 'Pixie Article', 'pixie-article' ),
'menu_name' => __( 'Pixie Articles', 'pixie-article' ),
'add_new' => _x( 'Add New', 'Pixie Article', 'pixie-article' ),
'add_new_item' => __( 'Add New Pixie Article', 'pixie-article' ),
'edit_item' => __( 'Edit Pixie Article', 'pixie-article' ),
'new_item' => __( 'New Pixie Article', 'pixie-article' ),
'view_item' => __( 'View Pixie Article', 'pixie-article' ),
'search_items' => __( 'Search Pixie Articles', 'pixie-article' ),
'not_found' => __( 'No pixie articles found', 'pixie-article' ),
'not_found_in_trash' => __( 'No pixie articles found in Trash', 'pixie-article' ),
'parent_item_colon' => __( 'Parent Pixie Article:', 'pixie-article' ),
'all_items' => __( 'All Pixie Articles', 'pixie-article' ),
);
$options = array(
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'menu_position' => 6,
'menu_icon' => 'dashicons-media-document',
'capability_type' => 'page',
'supports' => array( 'title', 'editor', 'thumbnail' ),
//// To re-implement admin_cols and admin_filters with stock WordPress API would require too much code to show here.
// 'admin_cols' => array(
// 'pixie_article_audience' => array(
// 'taxonomy' => 'pixie-article-audience',
// 'title' => _x( 'Audience', 'Pixie Article Taxonomy', 'pixie-article' ),
// ),
// 'post_date' => array(
// 'title' => __( 'Created', 'Date', 'pixie-article' ),
// 'post_field' => 'post_date',
// ),
// 'post_modified' => array(
// 'title' => __( 'Modified', 'Date', 'pixie-article' ),
// 'post_field' => 'post_modified',
// ),
// ),
// 'admin_filters' => array(
// 'pixie_article_audience' => array(
// 'taxonomy' => 'pixie-article-audience',
// 'title' => _x( 'Audience', 'Pixie Article Taxonomy', 'pixie-article' ),
// ),
// ),
////
'has_archive' => true,
'rewrite' => array(
'slug' => 'pixie-articles',
'with_front' => false,
),
);
register_post_type( 'pixie-article', $options );
$taxo_labels = array(
'name' => __( 'Pixie Article Audiences', 'pixie-article' ),
'singular_name' => __( 'Pixie Article Audience', 'pixie-article' ),
'search_items' => __( 'Search Pixie Article Audiences', 'pixie-article' ),
'popular_items' => __( 'Popular Pixie Article Audiences', 'pixie-article' ),
'all_items' => __( 'All Pixie Article Audiences', 'pixie-article' ),
'parent_item' => __( 'Parent Pixie Article Audience:', 'pixie-article' ),
'parent_item_colon' => __( 'Parent Pixie Article Audience:', 'pixie-article' ),
'edit_item' => __( 'Edit Pixie Article Audience', 'pixie-article' ),
'view_item' => __( 'View Pixie Article Audience', 'pixie-article' ),
'update_item' => __( 'Update Pixie Article Audience', 'pixie-article' ),
'add_new_item' => __( 'Add New Pixie Article Audience', 'pixie-article' ),
'new_item_name' => __( 'New Pixie Article Audience', 'pixie-article' ),
'separate_items_with_commas' => __( 'Separate Pixie Article Audiences with commas', 'pixie-article' ),
'add_or_remove_items' => __( 'Add or remove Pixie Article Audiences', 'pixie-article' ),
'choose_from_most_used' => __( 'Choose from most used Pixie Article Audiences', 'pixie-article' ),
'add_new' => _x( 'Add New', 'Pixie Article Audience', 'pixie-article' ),
'not_found' => __( 'No pixie article audiences found', 'pixie-article' ),
);
$taxo_options = array(
'labels' => $taxo_labels,
'public' => true,
'hierarchical' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'pixie-articles',
'with_front' => false,
),
);
register_taxonomy(
'pixie-article-audience',
'pixie-article',
$taxo_options
);
add_shortcode( 'pixie_articles', 'pixie_article_list' );
}
// Must use 'init' rather than earlier 'plugins_loaded' due to need to flush rewrite rules (white screen of death otherwise).
// Handled differently when using Extended CPTs and 'plugins_loaded' can be used in that case.
add_action( 'init', 'pixie_article_loaded' );
/**
* Handler for pixie_articles shortcode to list all Pixie Article titles.
*
* @return string
*/
function pixie_article_list() {
$output = '';
$posts_query = new WP_Query( 'post_type=pixie-article' );
if ( $posts_query->have_posts() ) {
$output .= '<div class="pixie-articles">';
while ( $posts_query->have_posts() ) {
$posts_query->the_post();
$output .= the_title( '', '<br />' );
}
$output .= '</div>';
}
wp_reset_postdata();
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment