Skip to content
Advertisement

How are submenu created in WordPress?

I am new to programming in php / WordPress and I have a problem with the add_submenu_page function, it doesn’t work.

This is the code I use.

<?php

defined('ABSPATH') or die("Bye bye");
define('LSC_RUTA',plugin_dir_path(__FILE__));

function insert_menu(){
    
    //Variables menĂº
    $nombre_pagina = 'LifeMiles Soap Client';
    $nombre_menu = 'Lifemiles Soap Client';
    $permisos = 'manage_options';
    $menu_slug = LSC_RUTA . "admin/configuracion.php";
    $function = '';
    $icon = 'dashicons-plugins-checked';
    
    add_menu_page($nombre_pagina, $nombre_menu, $permisos, $menu_slug, $function, $icon, 31);
}

function insert_submenu(){
    
    add_submenu_page(LSC_RUTA . "conectividad.php", 
        'Ajustes', 
        'Ajustes', 
        'manage_options', 
        LSC_RUTA . "admin/configuracion.php");
}

register_activation_hook( __FILE__, 'create_plugin_database_table' );

add_action( 'admin_menu', 'insert_menu' );
add_action( 'admin_menu', 'insert_submenu' );

?>

Can anybody help me?

Advertisement

Answer

I have already solved. I was wrong in the order of the variables.

The function has to receive the parameters in this order:

add_submenu_page (string $ parent_slug, string $ page_title, string $ menu_title, string $ capability, string $ menu_slug, callable $ function = '', int $ position = null);

And I did it like this:

add_submenu_page (string $ Menu_slug, string $ page_title, string $ menu_title, string $ capability, string $ parent_slug, callable $ function = '', int $ position = null);

The correct code is as follows:

<?php

defined('ABSPATH') or die("Bye bye");
define('LSC_RUTA',plugin_dir_path(__FILE__));

function insert_menu(){
    
        //Variables menĂº
        $nombre_pagina = 'LifeMiles Soap Client';
        $nombre_menu = 'Lifemiles Soap Client';
        $permisos = 'manage_options';
        $menu_slug = LSC_RUTA . "admin/configuracion.php";
        $function = '';
        $icon = 'dashicons-plugins-checked';
        
        add_menu_page($nombre_pagina, $nombre_menu, $permisos, $menu_slug, $function, $icon, 31);
        
        add_submenu_page($menu_slug,
            'Ajustes',
            'Ajustes',
            'manage_options',
            $menu_slug);
        
        add_submenu_page($menu_slug,
            'Prueba Conectividad',
            'Prueba Conectividad',
            'manage_options',
            LSC_RUTA . "conectividad.php");

}

register_activation_hook( __FILE__, 'create_plugin_database_table' );

add_action( 'admin_menu', 'insert_menu' );

?>

Regards.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement