How to modify a wordpress plugin’s menu title?

Asked

Viewed 1,383 times

2

I have a problem modifying the title of my plugin in the menu, I need the main title to be different from the second title, but I’m not getting.

As in the image:

Preciso que o título principal fique diferente do segundo título como na segunda imagem

My Code:

public function menu(){
    add_menu_page('Gallery', __('Gallery', 'Galleries'), 'manage_options', 'gallery', array('view', 'main'));
    add_submenu_page( 'gallery', 'My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug');
    add_submenu_page( 'gallery', 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-secondary-slug');
}

2 answers

2


You can manipulate the global variable $submenu and change that there (we also have the variable $menu, but it has no use here):

add_action( 'admin_menu', function()
{
    add_menu_page(
        'Gallery', 
        __('Gallery', 'Galleries'), 
        'manage_options', 
        'gallery', 
        function(){ echo '<h1>Principal</h1>'; } 
    );
    add_submenu_page( 
        'gallery', 
        'My Custom Page', 
        'My Custom Page', 
        'manage_options', 
        'slug-1', 
        function(){ echo '<h2>Segundo</h2>'; }
    );
    add_submenu_page( 
        'gallery', 
        'My Custom Submenu Page', 
        'My Custom Submenu Page', 
        'manage_options', 
        'slug-2', 
        function(){ echo '<h2>Terceiro</h2>'; }
    );

    // Trocar o nome de um submenu
    global $submenu;
    $submenu['gallery'][0][0] = 'Galerias';
});

submenu modificado

And here, a var_dump of the variable (summarised):

Array (
    [index.php] => Array (
            [0] => Array (
                    [0] => Home
                    [1] => read
                    [2] => index.php
                )
            [10] => Array (
                    [0] => Updates <span class='update-plugins count-5' title='4 Plugin Updates, 1 Theme Update'><span class='update-count'>5</span></span>
                    [1] => update_core
                    [2] => update-core.php
                )
        )
    [edit.php] => Array ( [...etc...] )
    [upload.php] => Array ( [...etc...] )
    [edit.php?post_type=page] => Array ( [...etc...] )
    [edit-comments.php] => Array ( [...etc...] )
    [themes.php] => Array ( [...etc...] )
    [plugins.php] => Array ( [...etc...] )
    [users.php] => Array ( [...etc...] )
    [tools.php] => Array ( [...etc...] )
    [options-general.php] => Array ( [...etc...] )
    [gallery] => Array (
            [0] => Array (
                    [0] => Gallery
                    [1] => manage_options
                    [2] => gallery
                    [3] => Gallery
                    [4] => menu-top menu-icon-generic toplevel_page_gallery
                    [5] => toplevel_page_gallery
                    [6] => none
                )
            [1] => Array (
                    [0] => My Custom Page
                    [1] => manage_options
                    [2] => slug-1
                    [3] => My Custom Page
                )
            [2] => Array (
                    [0] => My Custom Submenu Page
                    [1] => manage_options
                    [2] => slug-2
                    [3] => My Custom Submenu Page
                )
        )
)

1

When you use add_menu_page it automatically creates an item with the same name, you can use add_submenu_page and create an item called Galleries, and delete this Gallery, the code looks like this:

public function menu(){
    add_menu_page('Gallery', __('Gallery', 'Galleries'), 'manage_options', 'gallery', array('view', 'main'));
    add_submenu_page( 'gallery', 'Galleries', 'Galleries', 'manage_options', 'my-top-level-slug');
   remove_submenu_page('gallery','gallery');
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.