pavelbohovin
11/14/2015 - 9:39 PM

sequence.min.js will be loaded only if shortcode exists on page

sequence.min.js will be loaded only if shortcode exists on page

<?php

class wf_sequence_slider_shortcode {

	static $add_script;
	
	static function init () {
		add_shortcode( 'wf_sequence_slider', array( __CLASS__, 'wf_sequence_slider_func' ) );
		add_action( 'init', array( __CLASS__, 'register_script' ) );
		add_action( 'wp_footer', array( __CLASS__, 'print_script' ) );
	}
	
	static function wf_sequence_slider_func( $atts ) {
		self::$add_script = true;

    
    		// Shortcode logic here
		...
		
		
	}
	
	static function register_script() {
		wp_register_script( 'sequence', plugin_dir_url( dirname( dirname( __FILE__ ) ) ) . 'public/js/sequence.min.js' );
	}

	static function print_script() {
		if ( !self::$add_script ) return;
		wp_print_scripts( 'sequence' );
	}
}

wf_sequence_slider_shortcode::init();