jquery script not enqueued in child theme

There are two parts in the question I want to address:

  1. It didn’t work because the action uses a plural form: wp_enqueue_scripts, not wp_enqueue_script. I guess this happened, because the function you have to use here – wp_enqueue_script()does use a singular.

  2. You should not use the function wp_enqueue_script() with so many parameters.

    Register your scripts early, enqueue them late.

    function register_flaticon() {
        wp_register_script(
            'flaticon',
            get_stylesheet_directory_uri() . '/js/flaticon.js',
            array('jquery')
        );
    }
    function enqueue_flaticon() {
        wp_enqueue_script( 'flaticon' );
    }
    
    add_action( 'wp_loaded',          'register_flaticon' );
    add_action( 'wp_enqueue_scripts', 'enqueue_flaticon' );
    

    The reason for that is flexibility. There are four enqueue actions:

    • wp_enqueue_scripts
    • admin_enqueue_scripts
    • customize_controls_enqueue_scripts
    • login_enqueue_scripts

    If you register your script early enough, you or someone else can enqueue it then on one of the other actions, and collisions can be avoided, because WordPress will not register a script with the same handle twice. See also this thread about the naming convention for jQuery addons.

Leave a Comment