Snippet

Add Custom Toolbars in WordPress Admin Panel Easily

How to add custom toolbar in wordpress adminWordpress admin toolbar customizationAdd items to wordpress admin toolbarCustom shortcut links in wordpress toolbarCustomize wordpress admin panel toolbarWordpress admin toolbar pluginAdd submenu to wordpress admin toolsCustomize wordpress admin interfaceChange wordpress admin toolbar linksWordpress 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

1<?php 2// Hook into 'admin_bar_menu' to add custom items to the WordPress admin toolbar 3add_action('admin_bar_menu', 'wp_dudecom_customize_admin_toolbar', 100); 4 5function wp_dudecom_customize_admin_toolbar($wp_admin_bar) { 6 // Check if the current user has the capability to view the toolbar 7 if (!current_user_can('manage_options')) { 8 return; 9 } 10 11 // Add a parent item to the toolbar 12 $wp_admin_bar->add_node(array( 13 'id' => 'wp-dudecom-custom-toolbar', 14 'title' => 'WP-Dude Custom', 15 'href' => admin_url('admin.php?page=wp-dudecom-custom-page'), 16 'meta' => array( 17 'title' => __('WP-Dude Custom Page', 'textdomain'), // Tooltip 18 ), 19 )); 20 21 // Add a child item under the parent item 22 $wp_admin_bar->add_node(array( 23 'id' => 'wp-dudecom-custom-toolbar-child', 24 'parent' => 'wp-dudecom-custom-toolbar', 25 'title' => 'Custom Link', 26 'href' => 'https://example.com', 27 'meta' => array( 28 'title' => __('Visit Example', 'textdomain'), // Tooltip 29 'target' => '_blank', // Open in new tab 30 ), 31 )); 32} 33 34// Hook into 'admin_menu' to add a submenu under the Tools menu 35add_action('admin_menu', 'wp_dudecom_add_tools_submenu'); 36 37function wp_dudecom_add_tools_submenu() { 38 // Add a submenu item under the Tools menu 39 add_management_page( 40 __('WP-Dude Custom Page', 'textdomain'), // Page title 41 __('WP-Dude Custom', 'textdomain'), // Menu title 42 'manage_options', // Capability 43 'wp-dudecom-custom-page', // Menu slug 44 'wp_dudecom_custom_page_callback' // Callback function 45 ); 46} 47 48function wp_dudecom_custom_page_callback() { 49 // Output content for the custom page 50 echo '<div class="wrap">'; 51 echo '<h1>' . __('WP-Dude Custom Page', 'textdomain') . '</h1>'; 52 echo '<p>' . __('Welcome to the WP-Dude custom admin page!', 'textdomain') . '</p>'; 53 echo '</div>'; 54} 55?>

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.