SHARES

How To Enqueue Scripts and Styles in Plugin Only on Pages With a Shortcode

I was creating very simple plugin with shortcode but I had problem how to enqueue script only for pages where I used shortcode. After searching few web sites I found right solution here and this is the code:

function my_scripts() {

  wp_register_style('my_style', plugin_dir_url( __FILE__ ) . 'css/my_style.css' , array(), '1.0.0', 'all' );

  wp_register_style('my_script', plugin_dir_url(__FILE__) . 'css/my_script.css', array(), '1.0.0' );
}
add_action( 'init', 'my_scripts' );
    

function my_func( ) {

  wp_enqueue_style('my_style');
  wp_enqueue_script('my_script');

  return "shortcode content";

}	
add_shortcode( 'shortcode', 'my_func' );

 

Share on Facebook0Share on Google+0Tweet about this on Twitter