ShortCodeScriptLoader

Adapted from this excellent article: http://scribu.net/wordpress/optimal-script-loading.html.

Defined (1)

The class is defined in the following location(s).

/ShortCodeScriptLoader.php  
  1. abstract class ShortCodeScriptLoader extends ShortCodeLoader { 
  2.  
  3. var $doAddScript; 
  4.  
  5. public function register($shortcodeName) { 
  6. $this->registerShortcodeToFunction($shortcodeName, 'handleShortcodeWrapper'); 
  7.  
  8. // It will be too late to enqueue the script in the header,  
  9. // so have to add it to the footer 
  10. add_action('wp_footer', array($this, 'addScriptWrapper')); 
  11.  
  12. public function handleShortcodeWrapper($atts, $content = null) { 
  13. // Flag that we need to add the script 
  14. $this->doAddScript = true; 
  15. return $this->handleShortcode($atts, $content); 
  16.  
  17. // Defined in super-class: 
  18. //public abstract function handleShortcode($atts); 
  19.  
  20. public function addScriptWrapper() { 
  21. // Only add the script if the shortcode was actually called 
  22. if ($this->doAddScript) { 
  23. $this->addScript(); 
  24.  
  25. /** 
  26. * @abstract override this function with calls to insert scripts needed by your shortcode in the footer 
  27. * Example: 
  28. * wp_register_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true); 
  29. * wp_print_scripts('my-script'); 
  30. * @return void 
  31. */ 
  32. public abstract function addScript(); 
  33.