Skip to content

Instantly share code, notes, and snippets.

@BODA82
Last active November 14, 2024 05:56
Show Gist options
  • Select an option

  • Save BODA82/d7b6d6b2c2e9a4ad76315d3588aa40d4 to your computer and use it in GitHub Desktop.

Select an option

Save BODA82/d7b6d6b2c2e9a4ad76315d3588aa40d4 to your computer and use it in GitHub Desktop.
Add WordPress custom post type as submenu item of existing admin menu.
<?php
if (! function_exists('gist_register_cpt')) {
/**
* Register Custom Post Type
*/
function gist_register_cpt() {
// Set CPT labels
$labels = array(
'name' => _x('Items', 'Post Type General Name', 'textdomain'),
'singular_name' => _x('Item', 'Post Type Singular Name', 'textdomain'),
'menu_name' => __('Items', 'textdomain'),
'name_admin_bar' => __('Item', 'textdomain'),
'archives' => __('Item Archives', 'textdomain'),
'parent_item_colon' => __('Parent Item' . ':', 'textdomain'),
'all_items' => __('All Items', 'textdomain'),
'add_new_item' => __('Add New Item', 'textdomain'),
'add_new' => __('Add New', 'textdomain'),
'new_item' => __('New Item', 'textdomain'),
'edit_item' => __('Edit Item', 'textdomain'),
'update_item' => __('Update Item', 'textdomain'),
'view_item' => __('View Item', 'textdomain'),
'search_items' => __('Search Items', 'textdomain'),
'not_found' => __('Not found', 'textdomain'),
'not_found_in_trash' => __('Not found in Trash', 'textdomain'),
'featured_image' => __('Featured Image', 'textdomain'),
'set_featured_image' => __('Set featured image', 'textdomain'),
'remove_featured_image' => __('Remove featured image', 'textdomain'),
'use_featured_image' => __('Use as featured image', 'textdomain'),
'insert_into_item' => __('Insert into Item', 'textdomain'),
'uploaded_to_this_item' => __('Uploaded to this Item', 'textdomain'),
'items_list' => __('Items list', 'textdomain'),
'items_list_navigation' => __('Items list navigation', 'textdomain'),
'filter_items_list' => __('Filter Items list', 'textdomain'),
);
// Set CPT args
$args = array(
'label' => __('Item', 'textdomain'),
'description' => __('Custom Items Collection', 'textdomain'),
'labels' => $labels,
'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => false, // Set `show_in_menu` to false to prevent WP from adding a new parent menu item.
'menu_position' => 5,
'menu_icon' => 'dashicons-editor-code',
'show_in_admin_bar' => false,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page',
);
// Register the CPT
register_post_type('gist_post_type_key', $args);
}
add_action('init', 'gist_register_cpt', 0);
/**
* Add new CPT to an existing menu with add_submenu_page
*
* Docs: https://developer.wordpress.org/reference/functions/add_submenu_page/
*/
function gist_add_cpt_to_submenu() {
/**
* $parent_item is the slug of the parent menu item you want to place your CPT under.
*
* Possibilities:
* - Dashboard: ‘index.php’
* - Posts: ‘edit.php’
* - Media: ‘upload.php’
* - Pages: ‘edit.php?post_type=page’
* - Comments: ‘edit-comments.php’
* - Custom Post Types: ‘edit.php?post_type=your_post_type’
* - Appearance: ‘themes.php’
* - Plugins: ‘plugins.php’
* - Users: ‘users.php’
* - Tools: ‘tools.php’
* - Settings: ‘options-general.php’
* - Network Settings: ‘settings.php’
* - Admin Page: ’your-admin-page-slug’ (ex: admin.php?page=your-admin-page-slug)
*/
$parent_item = 'parent-slug';
add_submenu_page($parent_item, 'Items Page Title', 'Items Menu Name', 'edit_pages', 'edit.php?post_type=gist_post_type_key');
}
add_action('admin_menu', 'gist_add_cpt_to_submenu', 10); // If you have multiple submenu items, modify the priority to move it around.
}
@franitpro
Copy link
Copy Markdown

How can this be done for Jetengine CCT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment