Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display specific action from $wp_actions

  • Avatar of Slava UA
    Slava UA
    Participant

    @slaffik

    Hi Jeff,
    I looked into the code of your plugin and found that you didn’t use any special techniques to print all of the actions.
    I used this for my needs:

    global $wp_actions;
    $actions = $wp_actions;
    foreach ( $actions as $action ) {
    # echo $action . ‘
    ‘;
    if ( $action == ‘bp_adminbar_menus’ )
    $menus[] = $action;
    }
    print_r( $menus );

    As you can see I need only those running actions that hooked into do_action( ‘bp_adminbar_menus’ );
    The problem is that my version display only 73 actions. Yours – more than 1000. What’s wrong with my code? How can I get all actions hooked with bp_adminbar_menus?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Avatar of Jeff Sayre
    Jeff Sayre
    Participant

    @jeffsayre

    There is no reason to do anything special with the $wp_filter and $wp_actions array–other than deciding in which way you wish to display the output. WP Hook Sniffer creates 4 new, unique arrays, each that provide very specific, detailed information that you use to figure out what is going on.

    I’m not sure why you are bothering writing new code. All you need to do is look for the bp_adminbar_menus element within the $wp_filter array. You do that by making sure that you have selected to output the Action and Filter Function Array. Search that output for the subarray that actual shows the listing of all action functions that are hooked to the bp_adminbar_menus event.

    In the output from my setup, this is what I see for the bp_adminbar_menus subarray:


    bp_adminbar_menus:
    Array
    (
    [2] => Array
    (
    [bp_adminbar_login_menu] => Array
    (
    [function] => bp_adminbar_login_menu
    [accepted_args] => 1
    )

    )

    [4] => Array
    (
    [bp_adminbar_account_menu] => Array
    (
    [function] => bp_adminbar_account_menu
    [accepted_args] => 1
    )

    )

    [6] => Array
    (
    [bp_adminbar_blogs_menu] => Array
    (
    [function] => bp_adminbar_blogs_menu
    [accepted_args] => 1
    )

    )

    [8] => Array
    (
    [bp_adminbar_notifications_menu] => Array
    (
    [function] => bp_adminbar_notifications_menu
    [accepted_args] => 1
    )

    )

    [12] => Array
    (
    [bp_adminbar_authors_menu] => Array
    (
    [function] => bp_adminbar_authors_menu
    [accepted_args] => 1
    )

    )

    [20] => Array
    (
    [groups_setup_adminbar_menu] => Array
    (
    [function] => groups_setup_adminbar_menu
    [accepted_args] => 1
    )

    [xprofile_setup_adminbar_menu] => Array
    (
    [function] => xprofile_setup_adminbar_menu
    [accepted_args] => 1
    )

    )

    [100] => Array
    (
    [bp_adminbar_random_menu] => Array
    (
    [function] => bp_adminbar_random_menu
    [accepted_args] => 1
    )

    )

    )

    Avatar of Slava UA
    Slava UA
    Participant

    @slaffik

    The real problem is that I need to know all functions hooked into bp_adminbar_menus inside my plugin. I’m ablolutely rewriting AdminBar and to make everything sortable I need those hooks names. I got it via

    function cd_abb_adminbar_hooks() {
    global $wp_filter;
    foreach ( (array)$wp_filter as $key => $value ) {
    if ( $key == 'bp_adminbar_menus' ) {
    foreach ( (array)$value as $menu => $functions ) {
    foreach ( (array)$functions as $function ) {
    $adminbar[] = $function[ 'function' ];
    }
    }
    }
    }
    return $adminbar;
    }

    I don’t know how to make it less memore consuming :(
    Now I’m working on displaying everything back on AdminBar (I made remove_all_actions() after grabed all menus from there).

    Non-closed HTML tag bug reported as http://trac.buddypress.org/ticket/2359, FYI.

    Avatar of Jeff Sayre
    Jeff Sayre
    Participant

    @jeffsayre

    Paul, nice catch!

    I had not noticed this one before.

    Avatar of Jeff Sayre
    Jeff Sayre
    Participant

    @jeffsayre

    @slaFFik-

    I do understand what you need. Unfortunately, I do not see how your code will produce any different results than WP Hook Sniffer. The array $wp_filter is populated during page load. So, its contents may vary depending on which page you navigate. In general, it is possible for the contents of this array to be the same for many pages but then differ on a few. It all depends on the files that are loaded (via include, include_only, require, require_only) when the given page you are on is displayed.

    However, since the menu bar is displayed on all BP pages, I would expect that all tied-in action functions would be grabbed for every page. In fact, doing a quick search of the BuddyPress codebase, I discover a total of 8 action functions hooked to bp_adminbar_menus action event. That is what is being captured by the $wp_filter array. Look at the subarray I pasted in my last post above. It has 8 hooked action functions.

    Is there something else that you might be looking for instead? Do you have any 3rd-party plugins that might be hooking to the bp_adminbar_menus action event?

    Avatar of Slava UA
    Slava UA
    Participant

    @slaffik

    Jeff,
    Your plugin creates these and nothing else, but I need to use names of all the functions hooked into bp_adminbar_menus to insert data in db. I do this while activating my plugin and save results via add_option (can’t use directly in plugin, it rewrites those hooked just during installation so I need to be faster than my script :) ). On my installation I discovered 12 hooked action functions. And I need them all.
    And now the problem is: how can I discover automagically all functions hooked into bp_adminbar_menus while 3rd-party plugins are activating? Could you advice smth?

    Avatar of Jeff Sayre
    Jeff Sayre
    Participant

    @jeffsayre

    Okay, so what you are saying is that there are a total of 12 action functions that are hooked into the bp_adminbar_menus action event in your particular setup. That means that 4 of them are from 3rd-party themes and or plugins. Why do I say that? Because BuddyPress core only has 8 action functions tied to the bp_adminbar_menus action event.

    The WP Hook Sniffer will locate all of them, no mater from what source–core files, custom themes, or 3rd-party plugins under one simple condition. That condition is that the add_action calls are actually fired during page load. In other words it cannot locate add_action calls that get fired when a different page is loaded or add_action calls that are buried inside a function that does not get triggered unless a special criteria is met. To sniff out those added actions, you have to navigate to that page so that those calls get fired or cause the special criteria to be met so that the function containing the buried action call is triggered.

    Remember, the way that the WordPress Plugin API processes added and removed actions and filters is by storing (or deleting) a reference to them in the $wp_filter array. But that only happens for hooked functions that are actually encountered during page load. The $wp_filter array is wiped clean with each page load and rebuilt from scratch.

    Now, if you are saying that you have action functions that are supposed to be fired on a given page but you are not seeing that they’ve been fired, then that indicates that they are hooked to an action event that finishes firing before the file in which they are located even gets loaded. This is exactly what happened to me and caused me to write WP Hook Sniffer.

    I had several custom do_action calls and some add_action calls that were not firing. I could not figure out why. It turned out that BuddyPress had a few hooks (action hooks) that were tied to plugins_loaded and got fired before my files ever loaded. That meant by the time the files were loaded, it was too late as the action event was no longer triggering.

    If you think that might be the issue, then you need to look at the order in which action events are getting triggered. You look at the action functions listed in the subarray element of the $wp_filter array for the action event in question and then see if the action functions that you think should be fired are even listed in that subarray. If they are not, then that will tell you that there is some other action event that is messing up the firing sequence. The only other possibility is that the action functions simply do not get written to the $wp_filter array for that particular page you are viewing.

Viewing 7 replies - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.