0

How would I go about using the following script only when the widget is used?

<?php

//sidedish slide script
function sidedish_slide_script() {

    wp_register_script('add-sd-js', get_template_directory_uri() . '/form-slider/jquery-1.8.2.min.js', array('jquery'),'null', true );
    wp_register_script('add-sd-custom', get_template_directory_uri() . '/form-slider/formslider.js', array('jquery'),'null', true );
    wp_register_style('add-sd-css',get_stylesheet_directory_uri() . '/form-slider/styles.css', '','','screen' );


    wp_enqueue_script ('add-sd-js');
    wp_enqueue_script ('add-sd-custom');
    wp_enqueue_style ('add-sd-css');

}

add_action('wp_enqueue_scripts','sidedish_slide_script');


?>
0

1 Answer 1

1

is_widget_active is what you want e.g.

function sidedish_slide_script() {
 wp_register_script('add-sd-js', get_template_directory_uri() . '/form-slider/jquery-1.8.2.min.js', array('jquery'),'null', true );
 wp_register_script('add-sd-custom', get_template_directory_uri() . '/form-slider/formslider.js', array('jquery'),'null', true );
 wp_register_style('add-sd-css',get_stylesheet_directory_uri() . '/form-slider/styles.css', '','','screen' );

 if ( is_active_widget( false, 'widget_id', $this->id_base, true ) ) {
  wp_enqueue_script ('add-sd-js');
  wp_enqueue_script ('add-sd-custom');
  wp_enqueue_style ('add-sd-css');
 }
}
add_action('wp_enqueue_scripts','sidedish_slide_script');

https://codex.wordpress.org/Function_Reference/is_active_widget

2

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.