Question

I want to add one javascript on the wordpress pages where my sidebar is called i.e when I call sidebar like

<?php get_sidebar(); ?>

it should automatically include javascript on to that page. is there any such hook to which I can attach my javascript including code?

Was it helpful?

Solution

Try this code, this will call when any sidebar is called:--

add_action('get_sidebar', 'add_before_siderbar');
function add_before_siderbar($name)
{
    //Here `$name` is  name of side bar, it will blank if default sidebar is call.
    if($name == 'your-sidebar')
    {
        add_action( 'wp_enqueue_scripts', 'add_scripts' );
    }
}
function add_scripts()
{
    wp_enqueue_script( 'your-script', 'YOUR-SCRIPT-PATH', array( 'jquery' ), 'version', false);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top