0

I am trying to add an custom action hook in wordpress but it's not working.Please help me through this.

<?php
function wp_add_google_link(){
global $WP_Admin_Bar;
var_dump($WP_Admin_Bar);
$WP_Admin_Bar->add_menu(array(
    'id'=>'google_analytics',
    'title'=>'GoogleAnalytics',
    'href'=>'https://google.com/analytics'
    ));
}
add_action('wp_before_admin_bar_render','wp_add_google_link');
2
  • What are you trying to achieve? What does the code do now?
    – cjbj
    Sep 29, 2016 at 6:45
  • use wp_admin_bar instead $WP_Admin_Bar see detail here. anyway explain what really want ?
    – mlimon
    Sep 29, 2016 at 7:17

2 Answers 2

1

Nothing is wrong with your code. Just change the $Wp_Admin_Bar to all lowercase

   function wp_add_google_link(){
global $wp_admin_bar;
var_dump($wp_admin_bar);
$wp_admin_bar->add_menu(array(
    'id'=>'google_analytics',
    'title'=>'GoogleAnalytics',
    'href'=>'https://google.com/analytics'
    ));
}

add_action('admin_bar_menu','wp_add_google_link');
1

Your Entire code is correct and you have no errors in it.

AS for as the WordPress is considered it is case sensitive and it will through errors pointing out even though if it is case sensitive issues.

Ensure that you use the correct syntax and structures while handling with the Admin Side or as well as in the core file.

WP_Admin_Bar - WP_Admin_Bar is WordPress' class for generating the Toolbar that lines the top of WordPress sites when signed in. This class can be hooked and modified to add or remove options that appear in the admin bar.

Usage: This class is used internally by WordPress to create an object called $wp_admin_bar. Most modifications to WordPress toolbar will generally be done by modifying the $wp_admin_bar object that is passed through the admin_bar_menu hook.

Example:

add_action( 'admin_bar_menu', 'modify_admin_bar' );

function modify_admin_bar( $wp_admin_bar ){
global $wp_admin_bar;
  // do something with $wp_admin_bar;
}

Corrections that you have to make:

  1. Where ever you have used global $WP_Admin_Bar; you have to change it to global $wp_admin_bar;.

Note: Please note that the hooks are available for modifying this class but they have very limited use. In many cases, you will simply want to modify the $wp_admin_bar object.

And your Entire Code look like.

function wp_add_google_link(){
global $wp_admin_bar;
//var_dump($wp_admin_bar); If you need you remove the slash and check
$wp_admin_bar->add_menu(array(
    'id'=>'google_analytics',
    'title'=>'GoogleAnalytics',
    'href'=>'https://google.com/analytics'
    ));
}
add_action('admin_bar_menu','wp_add_google_link');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.