How to add the custom menu in the admin area in admin menus and admin bar in WordPress?

In this tutorial, we are going to learn how we can add the menu in the admin area and the top admin bar using the custom code.

Bellow has code to add the menu in the admin menu.

add_action('admin_menu', 'meal_planner_setup_menu');
function meal_planner_setup_menu(){
    add_menu_page( 'Plugin init page', 'Meal Planner', 'manage_options', 'meal-planner', 'plugin_init','dashicons-admin-page' );
    add_submenu_page( 'meal-planner', 'Add Plan', 'Add Plan', 'manage_options', 'meal-planner-slug', 'add_members_function');
admin menu

add bellow the code to display the page content.

function plugin_init(){
    echo "<h1>Hello Page!</h1>";

The code for we are going to add display the menu on the top admin bar with the help of that admin will be able to access the plugin item and menu item from the front end as well.

function create_dwb_menu() {
    global $wp_admin_bar;
    $menu_id = 'meal-planner';
    $wp_admin_bar->add_menu(array('id' => $menu_id, 'title' => __('Meal Planner'), 'href' => 'admin.php?page=meal-planner'));
    $wp_admin_bar->add_menu(array('id' => $menu_id, 'title' => __('Add Plan'), 'href' => 'admin.php?page=meal-planner-slug')); 
add_action('admin_bar_menu', 'create_dwb_menu', 2000);
admin bar menu