Site icon Code Solution

wp_enqueue_script was called incorrectly. Scripts and styles

The question:

I am not a developer, but I have a theme that is giving me the error wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks.

I have read some solutions where they suggest to change to do:

    function ppibfi_enqueue_scripts() {
   wp_enqueue_script( 'jquery' );
   wp_enqueue_script( 'thickbox' );
   wp_enqueue_script( 'media-upload' );
}
function ppibfi_enqueue_styles(){
    wp_enqueue_style( 'thickbox' );
}
add_action( 'wp_enqueue_scripts', 'ppibfi_enqueue_scripts' );
add_action( 'wp_enqueue_scripts', 'ppibfi_enqueue_styles' );

But the code in my website does not start with the same like a function it just states:

//load scripts
wp_enqueue_script('smoothscroll', get_bloginfo('stylesheet_directory').'/includes/js/smoothscroll.js', array('jquery'), '');
//wp_enqueue_script('carrosel', get_bloginfo('stylesheet_directory').'/js/infinite-carousel.js', array('jquery'), '');
wp_enqueue_script('marker', get_bloginfo('stylesheet_directory').'/includes/map/js/marker.js', array('jquery'), '');
//wp_enqueue_script('carrouse', get_bloginfo('stylesheet_directory').'/includes/js/carrousel.js', array('jquery'), '');
wp_enqueue_script('tinycar', get_bloginfo('stylesheet_directory').'/js/jquery.tinycarousel.min.js', array('jquery'), '');

and

// fix google map in the header issue
function googlemaphome() {
    if ( is_front_page()  ) {
       wp_register_script ('googlemaps', 'http://maps.google.com/maps/api/js?&sensor=false', false, '3');
    }
}
add_action('wp_enqueue_scripts', 'googlemaphome');

and 

add_action( 'wp_enqueue_scripts', 'jt_style_changer' );

 //load the responsive style
 function responsive_styles()
{ wp_enqueue_style('responsive', get_stylesheet_directory_uri() . '/styles/responsive.css',false,'1.0','all');}
add_action('wp_print_styles','responsive_styles');

Does anybody know how to rewrite so there is no error? Any help would be appreciated as I am not a developer.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Of course I’m unable to answer you exactly, because I don’t see a full functions.php code.
You can use wp_enqueue_scripts action hook with both, scripts and styles.
Do not call wp_enqueue_script and wp_enqueue_style outside of wp_enqueue_scripts hook.

add_action('wp_enqueue_scripts', 'wp_enqueue_scripts_callback');

function wp_enqueue_scripts_callback(){

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'thickbox' );
    wp_enqueue_script( 'media-upload' );

    wp_enqueue_script('smoothscroll', get_bloginfo('stylesheet_directory').'/includes/js/smoothscroll.js', array('jquery'), '');
    wp_enqueue_script('marker', get_bloginfo('stylesheet_directory').'/includes/map/js/marker.js', array('jquery'), '');
    wp_enqueue_script('tinycar', get_bloginfo('stylesheet_directory').'/js/jquery.tinycarousel.min.js', array('jquery'), '');

    wp_enqueue_style( 'thickbox' );

    if ( is_front_page()  ) {
        wp_register_script ('googlemaps', 'http://maps.google.com/maps/api/js?&sensor=false', false, '3');
    }

    wp_enqueue_style('responsive', get_stylesheet_directory_uri() . '/styles/responsive.css',false,'1.0','all');}

}

Just make sure you put it in a right order, because scripts have dependecies.

//here we enqueue jquery
wp_enqueue_script( 'jquery' );

//as example this script has jquery dependency
wp_enqueue_script('smoothscroll', get_bloginfo('stylesheet_directory').'/includes/js/smoothscroll.js', array('jquery'), '');

Also, you can create several callback functions if you prefer to divide scripts and style. It’s your choise.

function wp_enqueue_scripts_callback(){
    //wp_enqueue_script here
}

function wp_enqueue_styles_callback(){
    //wp_enqueue_style here
}

add_action('wp_enqueue_scripts', 'wp_enqueue_scripts_callback');
add_action('wp_enqueue_scripts', 'wp_enqueue_styles_callback');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Exit mobile version