Skip to content

Instantly share code, notes, and snippets.

@vladimirlukyanov
Created October 23, 2016 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladimirlukyanov/3ffc00ee111bc44a0f082fff4ae6e141 to your computer and use it in GitHub Desktop.
Save vladimirlukyanov/3ffc00ee111bc44a0f082fff4ae6e141 to your computer and use it in GitHub Desktop.
Shortcode style enqueue #2
<?php
class My_Shortcode {
static $add_script;
static function init() {
add_shortcode('myshortcode', array(__CLASS__, 'handle_shortcode'));
add_action('init', array(__CLASS__, 'register_script'));
add_action('wp_footer', array(__CLASS__, 'print_script'));
}
static function handle_shortcode($atts) {
self::$add_script = true;
// shortcode handling here
}
static function register_script() {
wp_register_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
}
static function print_script() {
if ( ! self::$add_script )
return;
wp_print_scripts('my-script');
}
}
My_Shortcode::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment