0

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.

5
  • Why do you use wp_enqueue_script outside of wp_enqueue_scripts hook?
    – anton
    May 22, 2021 at 8:12
  • I am not the developer of this theme, I just bought it and I have error, you give some suggestions how to correct the issue please
    – Abdinasir
    May 22, 2021 at 9:57
  • I don't know what's inside your functions.php, but you can leave just one function for wp_enqueue_scripts hook. I will call for example wp_enqueue_scripts_callback. I don't see admin checks, so I guess these scripts and styles go to front-end. add_action('wp_enqueue_scripts', 'wp_enqueue_scripts_callback'); - this hook will call your function. And put every wp_enqueue_script and wp_enqueue_style function calls inside wp_enqueue_scripts_callback function. function wp_enqueue_scripts_callback(){//your wp_enqueue_script and wp_enqueue_style should be here}
    – anton
    May 22, 2021 at 11:24
  • Thank you! I will try it out.
    – Abdinasir
    May 22, 2021 at 15:55
  • Is it then correct to do it like this? add_action('wp_enqueue_scripts', 'wp_enqueue_scripts_callback'); function wp_enqueue_scripts_callback() { wp_enqueue_style('responsive', get_stylesheet_directory_uri() . '/styles/responsive.css',false,'1.0','all'); add_action('wp_print_styles','responsive_styles'); add_action( 'wp_enqueue_scripts', 'jt_style_changer' ); add_action('wp_enqueue_scripts', 'googlemaphome'); }
    – Abdinasir
    May 22, 2021 at 17:41

1 Answer 1

0

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');
1
  • thank you very much!
    – Abdinasir
    May 23, 2021 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.