How to include old Javascript files into new WordPress site redesign

I am going to redesign an existing site and will be using WordPress. The current site is on a different CMS. There are a lot of Javascript slideshow type files in the current site and it would take a long to time to recreate all the files.

Is it fairly easy to add the js files inside a template? Do you just add them like you normally would?

Related posts

Leave a Reply

1 comment

  1. WordPress provides a better (recommended) way to add scripts using wp_enqueue_script function, for example, the following code (paste in your functions.php file) will add a my_custom_script.js file from yourThemeDirectory/js/ folder

    function add_my_script() {
        wp_enqueue_script(
            'custom-script',
            get_template_directory_uri() . '/js/my_custom_script.js',
            array( 'jquery' )
        );
    }
    
    add_action( 'wp_enqueue_scripts', 'add_my_script' );
    

    In this given example, the first argument is the handle/name for your script, second parameter is the full path with file name that you want to add and the third parameter is the script dependency and in this case WordPress will require for jQuery to be loaded before your my_custom_script.js loads.

    To load the jQuery simply you can use wp_enqueue_script( 'jquery' ) with add_action, because in WordPress some libraries are registered by default (including jQuery). Read the documentation for more details.