• Hi,

    In WordPress admin dashboard, I’d like to add a custom elements linking to an external URLs.

    I managed to add the red one using the code snippet below, but it outputs this url :
    https://domain.com/wp-admin/admin.php?page=menu_slug

    I’d like the menu to point to a custom external URL

    By default the menu is added as the last element, like the red one.
    I’d like to know how to place the menu item in the upper nav bar (like the green one), or how to place it in a certain position like the yellow one.

    This is the code snippet I used :

    add_action('admin_menu', 'custom_menu');
    
    function custom_menu() { 
    
      add_menu_page( 
          'Page Title', 
          'Custom Menu', 
          'edit_posts', 
          'menu_slug', 
          'page_callback_function', 
          'dashicons-media-spreadsheet' 
    
         );
    }

    Thanks for your help

Viewing 4 replies - 1 through 4 (of 4 total)
  • To add a custom menu to the admin bar do the following and adjust as necessary.

    add_action('admin_bar_menu', 'custom_menu', 100);
    
    function custom_menu($wp_admin_bar) {
        $args = array(
            'id'    => 'custom_menu',
            'title' => '<span class="ab-icon dashicons-welcome-write-blog"></span> Custom Menu', // title of the menu item with icon
            'href'  => 'https://www.deviant.media', // change URL here
            'meta'  => array(
                'class' => 'custom-menu-class', // you can add more classes here for styling
                'title' => 'Visit Deviant Media', // tooltip
                'target' => '_blank'
            )
        );
        $wp_admin_bar->add_node($args);
    }
    
    Thread Starter dscp8g

    (@dscp8g)

    Hi,

    Your solution shows me how to put the custom menu in the admin upper nav bar ??
    It seems though that the custom menu element is only displayed in desktop mode.

    I’d like to put the custom menu in the sidebar (red example), could you help put a custom URL using my code snippet ?
    Thanks again for your help.

    Moderator bcworkz

    (@bcworkz)

    Admin menu items contain relative URLs so you cannot directly link to an external resource. You could maybe patch up an item’s URL using JavaScript, but if someone clicks the link before the script can complete they’ll be taken to the initially defined admin page.

    An alternative would be to add a custom admin page as usual. Then have that page’s callback output a JS script that redirects to where you want to really go. Users will briefly see this admin page before the redirect will happen, so you might want to display a “Redirecting to…” message on the page. This way you are assured users will be taken to the desired location (unless they’ve disabled JS, which would break all sorts of WP functionality).

    You could actually do both. If the menu item URL replacement doesn’t work, the admin page redirect will work as a fallback.

    @dscp8g Try this:

    add_action('admin_menu', 'custom_menu');
    
    function custom_menu() {
        add_menu_page(
            'Page Title',              // The text to be displayed in the title tags of the page when the menu is selected
            'Custom Menu',             // The text to be used for the menu
            'manage_options',          // The capability required for this menu to be displayed to the user
            'custom-menu-slug',        // The slug name to refer to this menu by (should be unique for this menu)
            'custom_menu_page',        // The function to be called to output the content for this page
            'dashicons-welcome-write-blog', // Icon URL
            6                           // Position in the menu order
        );
    }
    
    function custom_menu_page() {
        // JavaScript to open the link in a new tab
        echo '<script type="text/javascript">
                window.open("https://www.deviant.media", "_blank");
              </script>';
        // Display a message on the page
        echo '<div class="wrap"><h1>External Link Opened</h1><p>The webpage has been opened in a new window/tab.</p></div>';
    }
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to add a custom link on admin dashboard ?’ is closed to new replies.