Add Custom Toolbars in WordPress Admin Panel Easily

How to add custom toolbar in wordpress admin; Wordpress admin toolbar customization; Add items to wordpress admin toolbar; Custom shortcut links in wordpress toolbar; Customize wordpress admin panel toolbar; Wordpress admin toolbar plugin; Add submenu to wordpress admin tools; Customize wordpress admin interface; Change wordpress admin toolbar links; Wordpress admin toolbar tips;

Explanation

Want to make your WordPress admin toolbar more useful? Here's a simple way to add custom links and pages to it.

Adding Custom Items to the Toolbar:

  • This code adds a new section to your admin toolbar, but only if you're an admin (someone who can manage options).
  • You'll see a new item called "WP-Dude Custom" in the toolbar. Clicking it takes you to a custom admin page.
  • Under this item, there's a link titled "Custom Link" that opens a new tab directing you to an external site, like example.com.

Creating a Custom Admin Page:

  • A new submenu item is added under the "Tools" menu in the admin panel, labeled "WP-Dude Custom".
  • When you click this submenu, it opens a custom page with a welcoming message.

These changes make it easier to access specific tools or external resources directly from your admin panel, streamlining your workflow.

Code

<?php
// Hook into 'admin_bar_menu' to add custom items to the WordPress admin toolbar
add_action('admin_bar_menu', 'wp_dudecom_customize_admin_toolbar', 100);

function wp_dudecom_customize_admin_toolbar($wp_admin_bar) {
    // Check if the current user has the capability to view the toolbar
    if (!current_user_can('manage_options')) {
        return;
    }

    // Add a parent item to the toolbar
    $wp_admin_bar->add_node(array(
        'id'    => 'wp-dudecom-custom-toolbar',
        'title' => 'WP-Dude Custom',
        'href'  => admin_url('admin.php?page=wp-dudecom-custom-page'),
        'meta'  => array(
            'title' => __('WP-Dude Custom Page', 'textdomain'), // Tooltip
        ),
    ));

    // Add a child item under the parent item
    $wp_admin_bar->add_node(array(
        'id'     => 'wp-dudecom-custom-toolbar-child',
        'parent' => 'wp-dudecom-custom-toolbar',
        'title'  => 'Custom Link',
        'href'   => 'https://example.com',
        'meta'   => array(
            'title' => __('Visit Example', 'textdomain'), // Tooltip
            'target' => '_blank', // Open in new tab
        ),
    ));
}

// Hook into 'admin_menu' to add a submenu under the Tools menu
add_action('admin_menu', 'wp_dudecom_add_tools_submenu');

function wp_dudecom_add_tools_submenu() {
    // Add a submenu item under the Tools menu
    add_management_page(
        __('WP-Dude Custom Page', 'textdomain'), // Page title
        __('WP-Dude Custom', 'textdomain'), // Menu title
        'manage_options', // Capability
        'wp-dudecom-custom-page', // Menu slug
        'wp_dudecom_custom_page_callback' // Callback function
    );
}

function wp_dudecom_custom_page_callback() {
    // Output content for the custom page
    echo '<div class="wrap">';
    echo '<h1>' . __('WP-Dude Custom Page', 'textdomain') . '</h1>';
    echo '<p>' . __('Welcome to the WP-Dude custom admin page!', 'textdomain') . '</p>';
    echo '</div>';
}
?>

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure you have administrator access to your WordPress site.
  • Familiarity with accessing and editing WordPress theme files or creating a custom plugin.

Implementation Steps:

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are editing the functions.php file, or use an FTP client to access your WordPress files if you are creating a custom plugin.
  3. Locate the functions.php file in your active theme directory, or create a new PHP file for your custom plugin.
  4. Copy and paste the provided code into the file.
  5. Save the changes to the file.
  6. Refresh your WordPress admin dashboard to see the new "WP-Dude Custom" toolbar item and the submenu under "Tools".

By following these steps, you can enhance your WordPress admin toolbar with custom links and pages, improving your workflow efficiency.

If you need further assistance or want to explore more advanced functionalities, consider reaching out to wp-dude.com for expert help.