Registering Menu on the WordPress Locally.

At first, there is no Menu Option on the “Appearance” option on WordPress.


function rubinastheme_register_menus() {
    register_nav_menus(
        array(
            'primary' => __('Primary Menu', 'rubinastheme'),
        )
    );
}
add_action('after_setup_theme', 'rubinastheme_register_menus');

To make it visible, we have copy paste the above code: This code is a part of a WordPress theme development process. Let’s break it down step by step:

  • Function Declaration: The code begins with the declaration of a function named rubinastheme_register_menus(). This function is intended to register navigation menus in the WordPress theme.
  • register_nav_menus() Function: Within the rubinastheme_register_menus() function, there’s a call to the register_nav_menus() function. This function is a WordPress core function used to register navigation menus to be displayed in various locations of a theme.
  • Parameters of register_nav_menus(): It accepts an array of parameters.
    • In this array, there’s one key-value pair:
    • Key: ‘primary’ – This is the identifier for the menu being registered. It’s common practice to use descriptive names here.
    • Value: (‘Primary Menu’, ‘rubinastheme’) – This is the human-readable label for the menu. (‘Primary Menu’, ‘rubinastheme’) is a localization function in WordPress used for translating strings. In this case, it’s used to provide a translatable string for the menu label. ‘rubinastheme’ is the text domain for translation, typically used to identify which theme or plugin the translation belongs to.
  • add_action(): After the function declaration, there’s a call to the add_action() function. This function is used in WordPress to attach a function to a specific action hook.
  • Parameters of add_action():
    • ‘after_setup_theme’ – This is the action hook to which the function rubinastheme_register_menus() is hooked. It’s fired after the theme is set up, allowing for theme initialization tasks.
    • ‘rubinastheme_register_menus’ – This is the name of the function that should be executed when the ‘after_setup_theme’ action hook is triggered.