• Resolved sheva29

    (@sheva29)


    Hello,

    I am trying to load two scripts using wp_enqueue_script(). Unfortunately only the first one loads but not the second one. Here is the code:

    //Load my own jQuery
    function fix_noconflict() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery' , 'https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js' );}
    
    add_action( 'wp_enqueue_scripts' , 'fix_noconflict' );
    
    //This two functions follow the same
    function mauricio_bootstrap_script_jquery() {
    
    //Includes bootstrap jQuery
    wp_register_script( 'custom-script', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap.js', array( 'jquery' ) );
    
    //This enqueus  the script
    
    wp_enqueue_script( 'custom-script' );
    }
    // Adds the new bootstrap function to the wp_enqueue_scripts
     add_action( 'wp_enqueue_scripts', 'mauricio_bootstrap_script_jquery' );
    
    function mauricio_bootstrap_script_carousel() {
    
    wp_register_script( 'myscript', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap-carousel.js', array( 'jquery' ) );
    
    wp_enqueue_script( 'myscript' );
    }
    
    add_action( 'wp_enqueue_script', 'mauricio_bootstrap_script_carousel' );

    Any suggestion will be more than welcome.

    Thanks,

    M

Viewing 11 replies - 1 through 11 (of 11 total)
  • Stop de-registering core jQuery. There is absolutely no need to do that.

    Thread Starter sheva29

    (@sheva29)

    Thanks esmi.

    I did it because I want to load the one from the snippets. I read that it causes less trouble. But that still does not solve my problem with the last function.

    I want to load the one from the snippets

    why?

    Thread Starter sheva29

    (@sheva29)

    As I said I heard that it causes less conflicts when loading scripts of your own.

    De-registering core jQuery will create a boatload of issues in WordPress. What happens if you re-write your code without de-registering jQuery?

    Thread Starter sheva29

    (@sheva29)

    I did, but the boostrap-carousel.js still does not load :(. Only bootstrap.js

    What code are you using now?

    Thread Starter sheva29

    (@sheva29)

    This:

    //This two functions follow the same
    function mauricio_bootstrap_script_jquery() {
    
    //Includes bootstrap jQuery
    wp_register_script( 'custom-script', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap.js', array( 'jquery' ) );
    
    //This enqueus  the script
    
    wp_enqueue_script( 'custom-script' );
    }
    // Adds the new bootstrap function to the wp_enqueue_scripts
     add_action( 'wp_enqueue_scripts', 'mauricio_bootstrap_script_jquery' );
    
    function mauricio_bootstrap_script_carousel() {
    
    wp_register_script( 'myscript', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap-carousel.js', array( 'jquery' ) );
    
    wp_enqueue_script( 'myscript' );
    }
    
    add_action( 'wp_enqueue_script', 'mauricio_bootstrap_script_carousel' );

    I completely got rid of the first function

    Try:

    function mauricio_bootstrap_scripts() {
    	wp_register_script( 'custom-script', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap.js', array( 'jquery' ) );
    	wp_enqueue_script( 'custom-script' );
    
    	wp_register_script( 'myscript', get_template_directory_uri().'/mauricio_bootstrap/js/bootstrap-carousel.js', array( 'jquery' ) );
    	wp_enqueue_script( 'myscript' );
    }
    add_action( 'template_redirect', 'mauricio_bootstrap_scriptsl' );

    Thread Starter sheva29

    (@sheva29)

    It worked! Thanks so much!

    Could you ellaborate on the $tag ‘template_redirect’. So if in the future I want to use two separete functions. I use ‘template_redirect’ instead of ‘wp_enqueue_script’. I noticed in the API section for plugins that there are a bunch of them.

    Best,

    Mauricio

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘wp_enqueue_script()’ is closed to new replies.