Conditional loading of script and css files

I am developing a small plugin which is supposed to implement a shortcode.
I understand how scripts and style files are enqued. Here comes the question...

Is it possible to arrange that plugin defined scripts and styles are loaded only on the posts and pages where plugin implemented shortcode is used?

  • Predrag Dubajic
    • Support

    Hey Balto,

    Hope you're doing well today :slight_smile:

    If you already registered your script and style you should be able to simply enqueue it directly with your shortcode like this:

    add_shortcode('my_awesome_shortcode', 'my_custom_shortcode');
    function my_custom_shortcode($atts) {
    	wp_enqueue_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
    	wp_enqueue_style( 'my-style' );
    	// actual shortcode here

    Is this what you're looking for? :slight_smile:

    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.