Support » Developing with WordPress » Javascript doesn’t load on wordpress pages added with wp_footer webhook

  • lieman

    (@lieman)


    Hello and thank you for your time.

    I’m building wordpress plugin, which should add some javascript code on every page of wordpress site.

    So i decided to register script and add it to page with wp_footer hook. But it doesn’t loaded on pages.

    I added var_dump() to check if is it registed and on every page, i got ” bool(true)” on all pages. So, hook should work, as i saw true, but the script doesn’t loaded.

    Can someone tell me, where i made a mistake?

    ___

    // code inside my lieman.php main plugin file
    function liemanAppend() {
        wp_register_script('lieman_code', 'https://lieman.ru/static/js/jquery.validate.min.js');
        wp_enqueue_script('lieman_code');
    
        var_dump(wp_script_is('lieman_code', 'enqueued'));
    }
    
    add_action('wp_footer', 'liemanAppend', 100000);
Viewing 2 replies - 1 through 2 (of 2 total)
  • Little Package

    (@littlepackage)

    This won’t work because of hook firing order. https://codex.wordpress.org/Plugin_API/Action_Reference You can’t make hooks that have already fired to fire again. Do something like this instead:

    // code inside my lieman.php main plugin file
    function liemanAppend() {
        wp_register_script('lieman_code', '/static/js/jquery.validate.min.js');
        wp_enqueue_script('lieman_code');
    
        var_dump(wp_script_is('lieman_code', 'enqueued'));
    }
    
    add_action('init', 'liemanAppend', 11 );

    Wordpress will put the scripts in the footer by default, if you want them in the footer you can use the wp_register_script or wp_enqueue_script function parameters to change that.

    Moderator bcworkz

    (@bcworkz)

    “wp_footer” is too late to properly enqueue. The proper action is “wp_enqueue_scripts”. You get the script to load in the footer by enqueuing or registering with a 5th arg ($in_footer) as true.
    https://developer.wordpress.org/reference/functions/wp_register_script/
    https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    If you are registering a script the uses jQuery, the 3rd arg ($deps) should at least be ['jquery',] to ensure the jQuery library gets loaded as well.

    FWIW, you don’t really need to register only to enqueue. Just provide all information when enqueuing. The reason to register is if you’d like to use the script’s handle elsewhere to refer to the registered script. No harm in registering anyway, but it’s often not necessary.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.