Created
October 23, 2016 01:26
-
-
Save vladimirlukyanov/3ffc00ee111bc44a0f082fff4ae6e141 to your computer and use it in GitHub Desktop.
Shortcode style enqueue #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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