Adding custom WordPress Dashboard menu item
Let’s look at how to create custom admin menu items for your WordPress site. The usage of Custom Post Types and options pages is another key aspect of creating a theme with WordPress.
Setting up function for Post Type
Let’s begin by writing the function to register the post type. Because this is an imaginary school website, I’m going to develop a post type called “course.” Obviously, what you use will change depending on the purpose of your custom post type.
add_action( 'init', 'course_register_post_type' );
The hook is the first parameter, and the call back function is the second. In the admin menu, you may alter the call back function to anything you like.
function course_register_post_type() {
}
Defining the Labels for Custom Post Type
Enter the below-listed code inside the curly bracket in the function to define labels for your custom post type. If your custom post type isn’t ‘course,’ you’ll need to alter it. You can use additional labels, but I feel that the ones listed above are sufficient for my purposes.
$labels = array(
'name' => _x( 'Course', 'post type general name' ),
'singular_name' => _x( 'Course', 'post type singular name' ),
'menu_name' => _x( 'Course', 'admin menu' ),
'name_admin_bar' => _x( 'Course', 'add new on admin bar' ),
'add_new' => _x( 'Add New', 'Course' ),
'add_new_item' => __( 'Add New Course' ),
'new_item' => __( 'New Course' ),
'edit_item' => __( 'Edit Course' ),
'view_item' => __( 'View Course' ),
'all_items' => __( 'All Course' ),
'search_items' => __( 'Search Course' ),
'parent_item_colon' => __( 'Parent Course:' ),
'not_found' => __( 'No Course found.' ),
'not_found_in_trash' => __( 'No Course found in Trash.' )
);
Defining the Arguments for Custom Post Type
$args = array(
'labels' => $labels,
'description' => __( 'Course pages' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'Coursehoroscope' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 11,
'menu_icon' => 'dashicons-star-empty',
'supports' => array( 'title', 'thumbnail' )
);
To add Gutenberg compatibility in your custom post type, it require two things
1. supports
must have editor
in it
2. show_in_rest
set to true
Adding the register_post_type() Function
The final step is to pull all of this together into the register_post_type()
function.
Below your arguments, and still inside the braces, add this:
register_post_type( 'course', $args );
Now you may save your work. In the admin menu, you’ll see that a new post type ‘course’ has emerged.
Also read Way On :Creating new custom post template in wordpress
You may also use WordPress Admin Menu Editor, Plugin to add custom menu on WordPress Dashboard.