WordPress and buddypress programming

Adding a Single Custom Shortcut Link to Toolbar With Code

Viewing 1 reply thread
  • Author
    Posts
    • #1478
      michael mc
      Keymaster
      @michael

      #wordpress Here’s another way to add a custom shortcut link to the WordPress toolbar. This method is for those who are comfortable copying code snippets into WordPress.

      You need to copy and paste the following code into your theme’s functions.php file or in a site-specific plugin.

      
      // add a link to the WP Toolbar
      function custom_toolbar_link($wp_admin_bar) {
          $args = array(
              'id' => 'wpbeginner',
              'title' => 'Search WPBeginner', 
              'href' => 'https://www.google.com:443/cse/publicurl?cx=014650714884974928014:oga60h37xim', 
              'meta' => array(
                  'class' => 'wpbeginner', 
                  'title' => 'Search WPBeginner Tutorials'
                  )
          );
          $wp_admin_bar->add_node($args);
      }
      add_action('admin_bar_menu', 'custom_toolbar_link', 999);

      This sample code adds a link to a Google Custom Search engine which will search for WordPress tutorials on WPBeginner. It uses the function add_node with the arguments described in the array.

    • #1479
      michael mc
      Keymaster
      @michael

      #wordpress
      Adding a Group of Custom Shortcut Links to Toolbar With Code
      The last method showed you how to add a custom link to the toolbar using code. But what if you want to create a custom menu with a handful of your own shortcuts?

      To do that you can group multiple shortcuts under one parent item. The child nodes under the parent link will appear when a user hovers their mouse on the parent link.

      Here’s an example of how to add a group of custom links in the WordPress toolbar. Like the previous method, you should copy and paste this code snippet into your theme’s functions.php file or in a site-specific plugin.

      /*
      * add a group of links under a parent link
      */
        
      // Add a parent shortcut link
        
      function custom_toolbar_link($wp_admin_bar) {
          $args = array(
              'id' => 'wpbeginner',
              'title' => 'WPBeginner', 
              'href' => 'https://www.wpbeginner.com', 
              'meta' => array(
                  'class' => 'wpbeginner', 
                  'title' => 'Visit WPBeginner'
                  )
          );
          $wp_admin_bar->add_node($args);
        
      // Add the first child link 
            
          $args = array(
              'id' => 'wpbeginner-guides',
              'title' => 'WPBeginner Guides', 
              'href' => 'https://www.wpbeginner.com/category/beginners-guide/',
              'parent' => 'wpbeginner', 
              'meta' => array(
                  'class' => 'wpbeginner-guides', 
                  'title' => 'Visit WordPress Beginner Guides'
                  )
          );
          $wp_admin_bar->add_node($args);
        
      // Add another child link
      $args = array(
              'id' => 'wpbeginner-tutorials',
              'title' => 'WPBeginner Tutorials', 
              'href' => 'https://www.wpbeginner.com/category/wp-tutorials/',
              'parent' => 'wpbeginner', 
              'meta' => array(
                  'class' => 'wpbeginner-tutorials', 
                  'title' => 'Visit WPBeginner Tutorials'
                  )
          );
          $wp_admin_bar->add_node($args);
        
      // Add a child link to the child link
        
      $args = array(
              'id' => 'wpbeginner-themes',
              'title' => 'WPBeginner Themes', 
              'href' => 'https://www.wpbeginner.com/category/wp-themes/',
              'parent' => 'wpbeginner-tutorials', 
              'meta' => array(
                  'class' => 'wpbeginner-themes', 
                  'title' => 'Visit WordPress Themes Tutorials on WPBeginner'
                  )
          );
          $wp_admin_bar->add_node($args);
        
      }
        
      add_action('admin_bar_menu', 'custom_toolbar_link', 999);
Viewing 1 reply thread
  • You must be logged in to reply to this topic.