4

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

//Load my own jQuery
function fix_noconflict() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery' , 'http://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' );

Just for the record I have wp_head() in my header. And as I said it loads the first function that includes bootstrap.js.

Thanks,

M

2 Answers 2

10

Why don´t you try to put all your functions inside a main function, like this?

function wpEnqueueScripts(){
    // Adds the new bootstrap function to the wp_enqueue_scripts
    wp_register_script('custom-script', get_template_directory_uri() . '/mauricio_bootstrap/js/bootstrap.js', array('jquery'));
    wp_enqueue_script('custom-script');

    // Adds the new bootstrap function to the wp_enqueue_scripts
    wp_register_script('myscript', get_template_directory_uri() . '/mauricio_bootstrap/js/bootstrap-carousel.js', array('jquery'));
    wp_enqueue_script('myscript');
}    

add_action('wp_enqueue_scripts', 'wpEnqueueScripts');
Sign up to request clarification or add additional context in comments.

Comments

2

Someone at the wordpress forum provided this. The two functions were combined and when adding the action the use of the 'template_redirect' $tag is used instead of the 'wp_enqueue_script'

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' );

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.