Can’t find where/how WordPress scripts are imported wp_head();

Hi all I have some trouble with a WordPress website.

I’m changing the way the scripts are loaded on the website (async vs sync) and doing some purge to useless scripts (multiple versions of jquery etc. etc.).
Some of them were loaded plain and simple on the header (header.php) and i have moved them already, some of the others are loaded with a function called wp_head();

Read More

This function loads a lot of stuff (scripts and plain text javascript) but i can’t find where it is…

i read there
How to find HTML rendered by <?php wp_head(); ?> in WordPress?
that there are many function hooked to this one by, for instace, plugins and more but how it is possible to find them all?

Anyone can help me?

Thanks

Related posts

Leave a Reply

1 comment

  1. Usually scripts are hooked in the wp_enqueue_scripts hook that is hooked in wp_head.
    I mean usually because not every developer follows the guidelines.

    You can check the functions hooked in wp_enqueue_scripts using this functions, that @Rarst created

    Put this in your functions.php:

    /**
     * A function to visually dump a hook.
     *
     * @param $tag
     * @param $hook
     */
    function cis_dump_hook($tag, $hook) {
        ksort($hook->callbacks);
    
        echo "<pre>>>>>>t$tag<br>";
    
        foreach ($hook->callbacks as $priority => $functions) {
    
            echo $priority;
    
            foreach ($functions as $function) {
                if ($function['function'] != 'list_hook_details') {
    
                    echo "t";
    
                    if(is_closure($function['function'])) {
                        echo "Closure: function(){}";
                    } else if (is_string($function['function'])) {
                        echo $function['function'];
                    } else if (is_string($function['function'][0])) {
                        echo $function['function'][0] . ' -> ' . $function['function'][1];
                    } else if (is_object($function['function'][0])) {
                        echo "(object) " . get_class($function['function'][0]) . ' -> ' . $function['function'][1];
                    } else {
                        print_r($function);
                    }
                    echo ' (' . $function['accepted_args'] . ') <br/>';
                }
            }
        }
        echo '</pre>';
    }
    
    function list_hooks( $filter = false ){
        global $wp_filter;
    
        $hooks = $wp_filter;
        ksort( $hooks );
    
        foreach( $hooks as $tag => $hook )
            if ( false === $filter || false !== strpos( $tag, $filter ) )
                dump_hook($tag, $hook);
    }
    

    and call it like this: list_hooks( 'wp_enqueue_scripts' );

    I really recommend to do this in a development environment.
    This will return a list of functions hooked in the wp_enqueue_scripts hook.
    To know more about the functions check the post Debug WordPress hooks.