Vim Bonsu
Vim Bonsu

Reputation: 1892

wordpress wp_enque_script not working

Trying to safely register and enque a few scripts but it's not working. When I view page source, i can see the scripts are loading but they don't work. If I include the scripts in the header or footer (the wrong way), they work just fine. Below is the code I have in my theme's functions.php file. Any thoughts?

if (function_exists('register_my_js')) {
function register_my_js()
{
    if ( !is_admin() )
  {

    wp_register_script( 'isotope', get_template_directory_uri() . '/js/isotope.min.js', array( 'jquery' ),'', true );

    wp_register_script( 'isotope-custom', get_template_directory_uri() . '/js/isotope-custom.js', array( 'jquery' ),'', true );

    wp_register_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.js', array( 'jquery' ),'', true );

    wp_register_script( 'plugins', get_template_directory_uri() . '/js/plugins.js', array( 'jquery' ),'', true );

    wp_register_script( 'common', get_template_directory_uri() . '/js/common.js', array( 'jquery' ),'', true );

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'isotope' );
    wp_enqueue_script( 'isotope-custom' );
    wp_enqueue_script( 'bootstrap' );
    wp_enqueue_script( 'plugins' );
    wp_enqueue_script( 'common' );

       }

    }
}

add_action('init', 'register_my_js');

Upvotes: 0

Views: 239

Answers (2)

Pieter Goosen
Pieter Goosen

Reputation: 9941

You are using the wrong hook. All stylesheets and jquery scripts should be hooked to wp_enqueue_scripts.

So change

add_action('init', 'register_my_js');

to

add_action('wp_enqueue_scripts', 'register_my_js');

Upvotes: 2

Mircearek
Mircearek

Reputation: 26

You need to change the first line as well, from function_exists to !function_exists. This way, the function will actually work. And I recommend you add

add_action('wp_enqueue_scripts', 'register_my_js');

before the last braket.

Upvotes: 0

Related Questions