WordPress Plugin to run on specific pages
I have developed a new WordPress plugin and I included a few CSS and JavaScript libraries in this.
I have noticed that these includes are loaded onto all pages not just the pages I have included the short code for my plugin. This will clearly impact performance loading unnecessary scripts/styles.
Is there a function within WordPress where you can set a condition on what pages a Wordpress plugin can run on?
I am loading in the scripts/CSS like this within my php:
function my_function() {
wp_register_script( 'example', plugins_url('/scripts/example.js', __FILE__ ) );
wp_register_style('example', plugins_url( '/example/example.css', __FILE__ ) );
wp_enqueue_style('example);
wp_enqueue_script('example');
}
add_action( 'wp_enqueue_scripts', 'my_function' );