Created
March 12, 2019 19:54
-
-
Save jonny-harte/46c19b530a57c4c43480d5c3699369f6 to your computer and use it in GitHub Desktop.
Function to add multiple wordpress custom post types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//https://codex.wordpress.org/Function_Reference/register_post_type | |
//https://developer.wordpress.org/resource/dashicons/ | |
$postTypes = [ | |
'Post Name' => [ | |
'menu_icon' => 'dashicons-businessman', | |
'menu_position' => 26, | |
'public' => true, | |
'rewrite' => ['slug' => 'name'], | |
'supports' => ['title', 'excerpt', 'editor', 'thumbnail'], | |
'show_in_rest' => true, | |
], | |
]; | |
function register_post_type($title, $args = []) | |
{ | |
if ($args['plural-ies']) { | |
$plual = 'ies'; | |
} else { | |
$plual = 's'; | |
} | |
$ucFirstTitle = ucfirst($title); | |
$lowercaseTitle = strtolower($title); | |
$sanitizedTitle = sanitize_title($title); | |
$defaults = [ | |
'labels' => [ | |
'add_new_item' => __('Add New ' . $ucFirstTitle, 'text-domain'), | |
'add_new' => _x('Add New', $lowercaseTitle, 'text-domain'), | |
'all_items' => __('All ' . ($args['plural-ies'] | |
? substr($ucFirstTitle, 0, -1) : $ucFirstTitle) | |
. $plual, | |
'text-domain'), | |
'edit_item' => __('Edit ' . $ucFirstTitle, 'text-domain'), | |
'menu_name' => _x(($args['plural-ies'] | |
? substr($ucFirstTitle, | |
0, -1) : $ucFirstTitle) . $plual, 'admin menu', | |
'text-domain'), | |
'name_admin_bar' => _x($ucFirstTitle, 'add new on admin bar', | |
'text-domain'), | |
'name' => _x(($args['plural-ies'] | |
? substr($ucFirstTitle, | |
0, -1) : $ucFirstTitle) . $plual, | |
'post type general name', 'text-domain'), | |
'new_item' => __('New ' . $ucFirstTitle, 'text-domain'), | |
'not_found_in_trash' => __('No ' . ($args['plural-ies'] | |
? substr($ucFirstTitle, 0, -1) : $ucFirstTitle) | |
. $plual . ' found in Trash.', 'text-domain'), | |
'not_found' => __('No ' . ($args['plural-ies'] | |
? substr($ucFirstTitle, 0, -1) : $ucFirstTitle) . $plual | |
. ' found.', | |
'text-domain'), | |
'parent_item_colon' => __('Parent ' . ($args['plural-ies'] | |
? substr($ucFirstTitle, 0, -1) : $ucFirstTitle) . $plual | |
. ':', | |
'text-domain'), | |
'search_items' => __('Search ' . ($args['plural-ies'] | |
? substr($ucFirstTitle, 0, -1) : $ucFirstTitle) | |
. $plual, | |
'text-domain'), | |
'singular_name' => _x($ucFirstTitle, | |
'post type singular name', 'text-domain'), | |
'view_item' => __('View ' . $ucFirstTitle, 'text-domain'), | |
], | |
]; | |
unset($args['plural-ies']); | |
$args = wp_parse_args($args, $defaults); | |
$postType = isset($args['postType']) ? $args['postType'] | |
: $sanitizedTitle; | |
register_post_type($postType, $args); | |
} | |
foreach ($postTypes as $name => $args) { | |
register_post_type($name, $args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment