Add menu and submenu in admin with a URL instead of slug?

I use this function and hook:

function mysite_admin_menu() 
{
  add_menu_page( 'Categories', 'Catégories', 'administrator', 'categories', 'a_function' );
  add_submenu_page( 'categories', 'Manage', 'Manage', 'administrator', 'xxx', 'a_function' );
  remove_submenu_page('categories','categories');
}
add_action( 'admin_menu', 'mysite_admin_menu' );

It displays what I need: a menu with a submenu on the left admin menu bar.

Read More

The thing is, the submenu leads to this page: admin.php?page=xxx.

How can I do to link to a URL like edit-tags.php?taxonomy=category?

If I swap the slug in the add_submenu_page with a relative URL, the link will lead to
admin.php?page=edit-tags.php?taxonomy=category.

Whatever I do, I always get admin.php?page=... which is not what I want.

Related posts

Leave a Reply

5 comments

  1. This is an old post but can’t you just use wordpress $menu and/or $submenu globals like Oleg suggested in number 2.

    When in doubt copy WordPress:

    wordpress/wp-admin/menu.php

    For example to add link this seems like it would work:

    function add_external_link_admin_submenu() {
        global $submenu;
        $permalink = admin_url( 'edit-tags.php' ).'?taxonomy=category';
        $submenu['options-general.php'][] = array( 'Manage', 'manage_options', $permalink );
    }
    add_action('admin_menu', 'add_external_link_admin_submenu');
    

    You can replace the $permalink = ... with anything

    So this should also work :

    $permalink = 'http://www.example.com';
    

    Also, it’s suggested not to use ‘administrator’ (even though I use it as well. Anyway, read this ticket for alternative solutions.

  2. I don’t recommend you do that.

    Let’s assume your prefix for admin.php is _trigger_me_

    1. Submenu

      <?php
      
      function _clean_url_to_repalce_admin_menu($url, $original_url, $_context){
          if ($url == 'admin.php?page=_trigger_me_'){
              remove_filter('clean_url', '_clean_url_to_repalce_admin_menu', 10);
              //return admin_url('someotherpage.php);
              return 'http://google.com/';
          }
      }
      if (is_admin())
      add_filter('clean_url', '_clean_url_to_repalce_admin_menu', 10, 3);
      
    2. Main Menu

      <?php
      function custom_admin_menu_action_hook(){
          global $menu;
          foreach($menu as $k=>$item){
              if ($item[2] == '_trigger_me_'){
                      $menu[$k][2] = 'http://google.com';
              }
          }
      }
      add_action('admin_menu', 'custom_admin_menu_action_hook', 100);
      

    I call both examples as “hacks”. They possible to do, but better to not do that.

    update.

    if case if satisfied with redirection =)

    <?php
    $hook = add_submenu_page($parent, $title, $menu_title, 'manage_option', 'callback');
    add_action("load-{$hook}", create_function('','
        header("Location:", admin_url("someurl.php?blahblahblah"));
        exit;
    '));
    

    in case if you want to substitute this by taxonomy page (edit-tags.php)

    <?php
    add_action('admin_menu', 'admin_menu_edit_tags_page');
    function admin_menu_edit_tags_page(){
        // please set $YOURPARENTSLUG as parent slug of your menu item (parent of your menu)
        // manage_tags to manage_terms of your taxonomy capability (its visible whan you var_dump($wp_taxonomies[$taxonomyyouwant]));
        add_submenu_page($YOURPARENTSLUG, 'menu', 'title', 'manage_tags',  'edit-tags.php?taxonomy=taxonomy');
    }
    
  3. If your submenu is missing the admin.php?page= prefix, you should register the parent menu first, then the submenu.

    add_action( 'admin_menu', function() {
    
    // Parent menu first
    add_menu_page( 'Page title', 'Menu Title', 'capability', 'parent-slug', 'callback' );
    
    // Then the submenus
    add_submenu_page( 'parent-slug', 'Submenu Page Title', 'Submenu Title', 'capability', 'slug', 'callback' );
    
    } );
    

    Otherwise the link will be just the slug.

  4. Thank you Oleg, your script works but breaks the admin interface (background colors and images all dissappear for any reason).

    But if I have to take this road, I can do this instead:

    function changeUrl()
    {
        $pageURL = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    
        if ($pageURL == 'http://www.mysite.com/wp-admin/admin.php?page=xxxx')
        {
            header ('location:http://www.mysite.com/wp-admin/edit-tags.php?taxonomy=category');
        }
    }
    add_action( 'admin_menu', 'changeUrl' );
    

    It’s seems curious to me that there is no simple way to link a submenu to any url.
    This kind of redirection is messy…but it works.

  5. Although this is an old problem, I’d like to contribute for consideration this code:

    function mysite_admin_menu() 
    {
      add_menu_page( 'Categories', 'Catégories', 'administrator', 'categories', 'a_function' );
      add_submenu_page( 'categories', 'Manage', 'Manage', 'administrator', 'manage_categories', 'mysite_manage_categories' );
      remove_submenu_page('categories','categories');
    }
    add_action( 'admin_menu', 'mysite_admin_menu' );
    
    function mysite_manage_categories() 
    { 
    
    ?><script>window.location = "<?php echo admin_url('edit-tags.php?taxonomy=category'); ?>";</script><?php 
        }
    

    it uses javascript redirect instead of creating another add_action to redirect via http.