1

i have multiple javascript file. How can i add those to wordpress by wp_register_script ? I can add single javascript file by

function wptuts_scripts_with_jquery() {
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/custom-script.js', array( 'jquery' ) );

    // For either a plugin or a theme, you can then enqueue the script:
    wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_jquery' );
3
  • Repeat the same for more scripts, you can't add multiple in one call AFAIK. Commented Aug 19, 2013 at 5:02
  • Thanks for your answer but i am not clear your answer. Can you give me a example ? Commented Aug 19, 2013 at 5:07
  • You have to repeat wp_register_script and wp_enqueue_script for each script you want to add. Commented Aug 19, 2013 at 5:10

1 Answer 1

4

You can create a common function for this and call it for every script like,

function wptuts_scripts_with_jquery()
{
    // create array of all scripts
    $scripts=array('custom-script'=>'/js/custom-script.js',
                  'custom-script1'=>'/js/custom-script1.js',
                  'custom-script2'=>'/js/custom-script2.js');
    foreach($scripts as $key=>$sc)
    {
       wp_register_script( $key, get_template_directory_uri() . $sc, array('jquery') );
       wp_enqueue_script( $key );
    }
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_jquery' );
Sign up to request clarification or add additional context in comments.

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.