Skip to content

Commit 57c2b4a

Browse files
committed
feat: add boilerplate CPT and taxonomy mu-plugins
1 parent e0c9e18 commit 57c2b4a

File tree

10 files changed

+359
-0
lines changed

10 files changed

+359
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Plugin Name: Custom Post Types
4+
* Description: Implements all custom post types
5+
* Version: 1.0.0
6+
* Author: Threespot
7+
* Author URI: https://www.threespot.com/
8+
* License: MIT License
9+
*/
10+
11+
// Gather all files from the post-types directory
12+
$files = glob(__DIR__ . '/post-types/*.php');
13+
14+
if (function_exists('register_extended_post_type')) {
15+
// Require each file
16+
foreach ($files as $file) {
17+
require_once $file;
18+
}
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
// Event custom post type definition
3+
//
4+
// Plugin docs: https://github.com/johnbillion/extended-cpts/wiki/Registering-Post-Types
5+
// WP docs: https://developer.wordpress.org/reference/functions/register_post_type/
6+
//
7+
// NOTE: We neeed to flush rewrite rules after making updates.
8+
// The simplest method is to go to Settings->Permalinks and click “Save Changes”
9+
// https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
10+
add_action('init', function() {
11+
register_extended_post_type('event', [
12+
// Dashicon: https://developer.wordpress.org/resource/dashicons/
13+
'menu_icon' => 'dashicons-calendar-alt',
14+
// Optional: Custom SVG icon
15+
// 'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode('<svg>…</svg>'),
16+
'supports' => [
17+
'author',
18+
'custom-fields',
19+
'editor',
20+
'excerpt',
21+
'revisions',
22+
'thumbnail',
23+
'title'
24+
],
25+
# FIXME: Add applicable custom taxonomies here
26+
'taxonomies' => [
27+
'category',
28+
'post_tag',
29+
'event_type',
30+
'topic',
31+
],
32+
'has_archive' => true,
33+
'hierarchical' => false,
34+
'publicly_queryable' => true,
35+
'show_in_rest' => true,
36+
'enter_title_here' => 'Event title',
37+
# Add taxonomy filter in the admin list view
38+
'admin_filters' => [
39+
'event_type' => [
40+
'taxonomy' => 'event_type',
41+
],
42+
],
43+
], [
44+
'singular' => 'Event',
45+
'plural' => 'Events',
46+
'slug' => 'events'
47+
]);
48+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
// Event custom post type definition
3+
//
4+
// Plugin docs: https://github.com/johnbillion/extended-cpts/wiki/Registering-Post-Types
5+
// WP docs: https://developer.wordpress.org/reference/functions/register_post_type/
6+
//
7+
// NOTE: We neeed to flush rewrite rules after making updates.
8+
// The simplest method is to go to Settings->Permalinks and click “Save Changes”
9+
// https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
10+
11+
// Example of how to display the template name in a custom admin column
12+
// https://github.com/johnbillion/extended-cpts/wiki/Admin-columns#custom-function
13+
// function get_template_name() {
14+
// global $post;
15+
// $current_template = get_page_template_slug($post);
16+
// $templates = wp_get_theme()->get_page_templates($post, 'news');
17+
// $template_name = $templates[$current_template] ?? 'Default';
18+
// echo $template_name;
19+
// };
20+
21+
add_action('init', function() {
22+
register_extended_post_type('news', [
23+
// Dashicon: https://developer.wordpress.org/resource/dashicons/
24+
'menu_icon' => 'dashicons-media-document',
25+
// Optional: Custom SVG icon
26+
// 'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode('<svg>…</svg>'),
27+
'supports' => [
28+
'author',
29+
'custom-fields',
30+
'editor',
31+
'excerpt',
32+
'revisions',
33+
'thumbnail',
34+
'title'
35+
],
36+
# FIXME: Add applicable custom taxonomies here
37+
'taxonomies' => [
38+
'category',
39+
'post_tag',
40+
'topic',
41+
],
42+
'has_archive' => true,
43+
'hierarchical' => false,
44+
'publicly_queryable' => true,
45+
'show_in_rest' => true,
46+
'enter_title_here' => 'Title',
47+
# Add taxonomy filter in the admin list view
48+
'admin_filters' => [
49+
'event_type' => [
50+
'taxonomy' => 'news_type',
51+
],
52+
],
53+
// 'admin_cols' => [
54+
// 'template' => [
55+
// 'title' => 'Template',
56+
// 'function' => 'get_template_name',
57+
// ],
58+
// 'last_modified' => [
59+
// 'title' => 'Last Modified',
60+
// 'post_field' => 'post_modified',
61+
// 'date_format' => 'Y/m/d \a\t g:i\&\n\b\s\p\;a',
62+
// ],
63+
// 'author' => [
64+
// 'title' => 'Author',
65+
// 'meta_key' => 'author',
66+
// ],
67+
// ]
68+
], [
69+
'singular' => 'News',
70+
'plural' => 'News',
71+
'slug' => 'news'
72+
]);
73+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
// People custom post type definition (e.g. staff and board)
3+
//
4+
// Plugin docs: https://github.com/johnbillion/extended-cpts/wiki/Registering-Post-Types
5+
// WP docs: https://developer.wordpress.org/reference/functions/register_post_type/
6+
//
7+
// NOTE: We neeed to flush rewrite rules after making updates.
8+
// The simplest method is to go to Settings->Permalinks and click “Save Changes”
9+
// https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
10+
add_action('init', function() {
11+
register_extended_post_type('person', [
12+
// Dashicon: https://developer.wordpress.org/resource/dashicons/
13+
'menu_icon' => 'dashicons-id-alt',
14+
// Optional: Custom SVG icon
15+
// 'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode('<svg>…</svg>'),
16+
'supports' => [
17+
'author',
18+
'custom-fields',
19+
'editor',
20+
'excerpt',
21+
'revisions',
22+
'thumbnail',
23+
'title'
24+
],
25+
# FIXME: Add applicable custom taxonomies here
26+
'taxonomies' => [
27+
'person_type',
28+
],
29+
'has_archive' => true,
30+
'hierarchical' => false,
31+
'publicly_queryable' => true,
32+
'show_in_rest' => true,
33+
'enter_title_here' => 'Enter full name',
34+
# Add taxonomy filter in the admin list view
35+
'admin_filters' => [
36+
'person_type' => [
37+
'taxonomy' => 'person_type',
38+
],
39+
],
40+
], [
41+
'singular' => 'Person',
42+
'plural' => 'People',
43+
'slug' => 'people',
44+
]);
45+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Plugin Name: Custom Taxonomies
4+
* Description: Implements all custom taxonomies
5+
* Version: 1.0.0
6+
* Author: Threespot
7+
* Author URI: https://www.threespot.com/
8+
* License: MIT License
9+
*/
10+
11+
// Gather all files from the post-types directory
12+
$files = glob(__DIR__ . '/taxonomies/*.php');
13+
$files = array_merge($files, glob(__DIR__ . '/query-vars/*.php'));
14+
15+
if (function_exists('register_extended_taxonomy')) {
16+
// Require each file
17+
foreach ($files as $file) {
18+
require_once $file;
19+
}
20+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* Build a custom query based on several conditions
5+
* The pre_get_posts action gives developers access to the $query object by reference
6+
* any changes you make to $query are made directly to the original object - no return value is requested
7+
*
8+
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
9+
*
10+
*/
11+
add_action('pre_get_posts', function ($query)
12+
{
13+
if (
14+
// Don’t alter the admin queries
15+
is_admin() ||
16+
// Only alter the main query
17+
!$query->is_main_query() ||
18+
// Only on search and archive pages
19+
!($query->is_search() || $query->is_archive())
20+
) {
21+
return;
22+
}
23+
24+
$tax_query = [];
25+
26+
// List custom taxonomies that should support query vars
27+
// Note: We also need to update the list in /custom-taxonomies/query-vars/filters.php
28+
$taxonomies = [
29+
'event_type',
30+
'person_type',
31+
'topic',
32+
];
33+
34+
// Add custom taxonomy term support
35+
foreach ($taxonomies as $taxonomy) {
36+
$query_var = get_query_var($taxonomy);
37+
if (!empty($query_var)) {
38+
array_push($tax_query, [
39+
'taxonomy' => $taxonomy,
40+
'field' => 'slug',
41+
'terms' => $query_var,
42+
'operator' => 'IN',// 'IN', 'NOT IN', 'AND', 'EXISTS' and 'NOT EXISTS'
43+
'include_children' => false,
44+
]);
45+
}
46+
}
47+
48+
if (count($tax_query) > 1) {
49+
$tax_query['relation'] = 'AND';
50+
}
51+
52+
if (count($tax_query) > 0) {
53+
$query->set('tax_query', $tax_query);
54+
}
55+
56+
// Order events by date
57+
if ($query->get('post_type') == 'event' && empty($query->get('orderby'))) {
58+
$query->set('order', 'DESC');
59+
$query->set('orderby', 'date');
60+
}
61+
62+
}, 1);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Register custom query vars
4+
*
5+
* @link https://developer.wordpress.org/reference/hooks/query_vars/
6+
* @link https://www.smashingmagazine.com/2016/03/advanced-wordpress-search-with-wp_query/
7+
*/
8+
add_filter('query_vars', function($vars) {
9+
// FIXME: List custom taxonomies below that should support query vars
10+
// Note: We also need to update the list in /custom-taxonomies/query-vars/actions.php
11+
$taxonomies = [
12+
'event_type',
13+
'person_type',
14+
'topic',
15+
];
16+
17+
foreach ($taxonomies as $taxonomy) {
18+
$vars[] = $taxonomy;
19+
}
20+
21+
return $vars;
22+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
// Event type taxonomy
3+
// Docs: https://github.com/johnbillion/extended-cpts/wiki/Registering-taxonomies
4+
add_action('init', function () {
5+
register_extended_taxonomy('event_type', [
6+
'event',
7+
], [
8+
'exclusive' => true,
9+
'hierarchical' => false,
10+
'meta_box' => 'dropdown',// 'radio', 'dropdown', 'simple' (supports multiple)
11+
'public' => true,// applies to “publicly_queryable”, “show_ui”, and “show_in_nav_menus”
12+
'query_var' => true,
13+
'required' => false,
14+
'show_admin_column' => true,
15+
'show_in_quick_edit' => true,
16+
'show_in_rest' => true,
17+
], [
18+
'singular' => 'Event Type',
19+
'plural' => 'Event Types',
20+
'slug' => 'event-type',
21+
]);
22+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
// Custom taxonomy for people (e.g. staff, board of directors)
3+
// Docs: https://github.com/johnbillion/extended-cpts/wiki/Registering-taxonomies
4+
add_action('init', function () {
5+
register_extended_taxonomy('person_type', [
6+
'person',
7+
], [
8+
'checked_ontop' => true,// useful when there are many terms
9+
'exclusive' => true,
10+
'hierarchical' => false,
11+
'meta_box' => 'dropdown',// 'radio', 'dropdown', 'simple' (supports multiple)
12+
'public' => true,// applies to “publicly_queryable”, “show_ui”, and “show_in_nav_menus”
13+
'query_var' => true,
14+
'required' => true,
15+
'show_admin_column' => true,
16+
'show_in_quick_edit' => true,
17+
'show_in_rest' => true,
18+
], [
19+
'singular' => 'Person Type',
20+
'plural' => 'Person Types',
21+
'slug' => 'person-type',
22+
]);
23+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
// Imapct Area taxonomy
3+
// Docs: https://github.com/johnbillion/extended-cpts/wiki/Registering-taxonomies
4+
add_action('init', function () {
5+
register_extended_taxonomy('topic', [
6+
'post',
7+
'page',
8+
'event',
9+
'news'
10+
], [
11+
'exclusive' => false,
12+
'hierarchical' => false,
13+
'meta_box' => 'simple',// 'radio', 'dropdown', 'simple' (supports multiple)
14+
'public' => true,// applies to “publicly_queryable”, “show_ui”, and “show_in_nav_menus”
15+
'query_var' => true,
16+
'required' => false,
17+
'show_admin_column' => true,
18+
'show_in_quick_edit' => true,
19+
'show_in_rest' => true,
20+
], [
21+
'singular' => 'Topic',
22+
'plural' => 'Topics',
23+
'slug' => 'topic',
24+
]);
25+
});

0 commit comments

Comments
 (0)