Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A tiny little article plugin to demo Extended CPTs ... with translatable labels, menu icon, custom taxonomy, admin columns, admin filters and shortcode.
<?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 Extended CPTs.
* 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_loaded() {
require_once( 'extended-cpts/extended-cpts.php' );
require_once( 'extended-taxos/extended-taxos.php' );
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,
'menu_icon' => 'dashicons-media-document',
'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' ),
),
),
);
register_extended_post_type( 'pixie-article', $options );
register_extended_taxonomy( 'pixie-article-audience', 'pixie-article' );
add_shortcode( 'pixie_articles', 'pixie_article_list' );
}
add_action( 'plugins_loaded', '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