Skip to content

Instantly share code, notes, and snippets.

@LeoSeyers
Created September 21, 2022 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeoSeyers/f820ef1d6ec2a4100e041f5fd7e6ee9e to your computer and use it in GitHub Desktop.
Save LeoSeyers/f820ef1d6ec2a4100e041f5fd7e6ee9e to your computer and use it in GitHub Desktop.
Create custom post type
<?php
/**
* Register CPT
*
* @return void
*/
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
add_action('init', function () {
$context = "bme";
$cpt_name = 'Élément';
$cpt_lowername = clean(strtolower($cpt_name));
$dash = "dashicons-admin-home";
$supports = ['title', 'editor', 'thumbnail'];
$labels = array(
'name' => _x($cpt_name, $context),
'singular_name' => _x($cpt_name, $context),
'add_new' => _x('Add new', $context),
'add_new_item' => _x("Add new $cpt_name", $context),
'edit_item' => _x("Edit $cpt_name", $context),
'new_item' => _x("New $cpt_name", $context),
'view_item' => _x("View $cpt_name", $context),
'search_items' => _x("Search $cpt_name", $context),
'not_found' => _x("$cpt_name not found", $context),
'not_found_in_trash' => _x("$cpt_name not found in trash", $context),
'parent_item_colon' => _x("Parent $cpt_name :", $context),
'menu_name' => _x("$cpt_name", $context),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => "Elements",
'supports' => $supports,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'menu_position' => 3,
'show_in_nav_menus' => true,
'exclude_from_search' => false,
'has_archive' => false,
'capability_type' => 'post',
'menu_icon' => $dash
);
register_taxonomy(
$cpt_lowername . '-' . 'category',
array($cpt_lowername),
array(
'hierarchical' => false,
'label' => 'Categories',
'singular_label' => 'Category',
'show_in_rest' => true,
)
);
register_taxonomy(
$cpt_lowername . '-' . 'tag',
array($cpt_lowername),
array(
'hierarchical' => false,
'label' => 'Tags',
'singular_label' => 'Tag',
'show_in_rest' => true,
)
);
register_post_type($cpt_lowername, $args);
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment