2

How to get (loop through to disable unnecessary) all functions with priority '1' attached to hook 'wp_head' ('_wp_render_title_tag', 'wp_enqueue_scripts', 'noindex' etc.)?

It is necessary to get it automatically, so that after each new version of WordPress do not keep track of whether the priority '1' something else.

Something like:

$array_priority_1 = [];

if (doing_action('wp_head')) {
  // Here we get and iterate over all functions related to 'wp_head'
  // and immediately check their priority '1'?
  // suitable and added to the array '$array_priority_1'
  //
  // ...I do not know how to get them and check for priority
}

And here we are going through our ready array:

foreach ($array_priority_1 as ...

Thank you!

0

The global $wp_filter store the hooks and have also his arguments, include the priority. So you can create a small function to debug them. The follow function should helps to list all functions, his arguments if fired on wp_head. The foreach loop add the priority of a hook via the run, the doing. So you can list all functions for the hook wp_head with priority 10 as example via var_dump($hooked[0][10]);.

    add_action('all', function($hook) {

        if (doing_action('wp_head')) {

            global $wp_filter;
            if ( ! isset( $wp_filter[ $hook ] ) ) {
                return;
            }

            $hooked = (array) $wp_filter[ $hook ];
            foreach ( $hooked as $priority => $function ) {
                // Prevent buffer overflows of PHP_INT_MAX on array keys.
                // So reset the array keys.
                $hooked   = array_values( $hooked );
                $hooked[] = $function;
            }
            // Print all functions with priority `1`.
            print '<pre>'; print_r($hooked[0][1]); print '</pre>';
        }

        return $hook;
    });

Result

For my test example print all functions with priority 1

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

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

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

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

)

Additional hint, source

Also this function inside a debug helper plugin should helps you to understand it - https://github.com/bueltge/debug-objects/blob/master/inc/class-page_hooks.php#L90

6
  • You've ignored the priority part of the question, I guess? Jan 4 '19 at 10:00
  • The priority is inside the arguments, in my example inside the var $hooked.
    – bueltge
    Jan 4 '19 at 10:44
  • But you don't do anything with it. Your code will print all functions hooked to wp_head, not only these with given priority (as asked in question)... Or have I overlooked something? Jan 4 '19 at 10:46
  • Yes, so I have explained the explanation and the code, include an example to understand easier.
    – bueltge
    Jan 4 '19 at 11:03
  • Then please send the hook as right answer to help others to see, it works.
    – bueltge
    Jan 4 '19 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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