Snippet

Modify Links in WordPress Admin Toolbar Easily

How to add custom links to wordpress admin toolbarCustomize wordpress admin bar linksAdd shortcut links to wordpress toolbarModify links in wordpress admin barCustomize admin toolbar in wordpressAdd links to wordpress admin bar without pluginChange wordpress toolbar linksWordpress admin bar link customizationHow to edit wordpress admin toolbarAdd custom shortcut to wordpress admin bar

Explanation

Want to add your own links to the WordPress admin toolbar? This code snippet does just that, making it easier to access your favorite pages directly from the toolbar.

Here's what it does:

  • First, it checks if the user has the right permissions (like an admin) to see the toolbar changes. If not, it skips the rest.
  • It adds a new link to the toolbar. This link can point to any URL you want, like an external website. You can customize the link's title, tooltip, and even make it open in a new tab.
  • It also adds a sub-link under the existing "site-name" menu in the toolbar. This one links to the Posts page in your WordPress admin area, but you can change it to any admin page you prefer.

To make it your own:

  • Change the URL in the first link to wherever you want it to go.
  • Adjust the titles and tooltips to better describe your links.
  • Feel free to add more links by copying the structure and modifying as needed.

This is a handy way to customize your WordPress admin experience without needing a plugin!

Code

1<?php 2// Hook into 'admin_bar_menu' to add custom links to the WordPress admin toolbar 3add_action('admin_bar_menu', 'wp_dudecom_customize_admin_bar', 100); 4 5function wp_dudecom_customize_admin_bar($wp_admin_bar) { 6 // Check if the current user has the capability to view the admin bar 7 if (!current_user_can('manage_options')) { 8 return; 9 } 10 11 // Add a custom link to the admin toolbar 12 $wp_admin_bar->add_node(array( 13 'id' => 'wp-dudecom-custom-link', 14 'title' => 'Custom Link', 15 'href' => 'https://example.com', // Replace with your desired URL 16 'meta' => array( 17 'title' => __('Visit Custom Link'), // Tooltip text 18 'target' => '_blank', // Open link in a new tab 19 'class' => 'wp-dudecom-custom-class' // Custom CSS class 20 ), 21 )); 22 23 // Add another custom link under the existing 'site-name' node 24 $wp_admin_bar->add_node(array( 25 'parent' => 'site-name', 26 'id' => 'wp-dudecom-sub-link', 27 'title' => 'Sub Link', 28 'href' => admin_url('edit.php'), // Link to the Posts page in the admin area 29 'meta' => array( 30 'title' => __('Go to Posts'), // Tooltip text 31 ), 32 )); 33} 34?>

Instructions

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

Prerequisites:

  • Ensure you have access to your WordPress theme files or can create/edit a custom plugin.
  • Have administrative access to your WordPress site.

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 go to Plugins > Add New to create a custom plugin.
  3. If using functions.php, select the active theme's functions.php file from the right-hand side.
  4. Copy the provided code snippet.
  5. Paste the code at the end of the functions.php file or in your custom plugin file.
  6. Modify the 'href' value in the 'wp-dudecom-custom-link' node to your desired URL.
  7. Adjust the 'title' and 'meta' values to customize the link's appearance and behavior.
  8. Save the changes to the file.
  9. Refresh your WordPress admin dashboard to see the new links in the admin toolbar.

Note: Always back up your site before making changes to theme or plugin files.

If you need assistance with implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert help.