When we need to create theme or plugin from the scratch or need to extend the parent theme functionality we have to add javascript file the wordpress. Here we will leard how to add a simple jQuery script to WordPress:

1. How do I add a simple JavaScripts to Parent Theme

add_action( 'wp_enqueue_scripts', 'add_codecanal_parent_script' );
function add_codecanal_parent_script() {
    wp_register_script(
        'parent-theme-script', 
        get_template_directory_uri() . '/js/your-script.js', 
        array('jquery') 
    );

    wp_enqueue_script('parent-theme-script');
}

2. How do I add a simple JavaScripts to Child Theme

add_action( 'wp_enqueue_scripts', 'add_codecanal_child_script' );
function add_codecanal_child_script() {
    wp_register_script(
       'child-theme-script', 
       get_stylesheet_directory_uri() . '/js/your-script.js', 
       array('jquery') 
    );

    wp_enqueue_script('child-theme-script');
}

3. How do I add a simple JavaScripts to Plugin

add_action( 'wp_enqueue_scripts', 'add_codecanal_plugin_script' );
function add_codecanal_plugin_script() {
    wp_register_script(
       'plugin-script', 
       plugins_url . '/js/your-script.js', 
       array('jquery') 
    );

    wp_enqueue_script('plugin-script');
}
There are currently no comments.