Last active
May 30, 2016 09:36
-
-
Save organizm/ede6ee70d1805426f4a0 to your computer and use it in GitHub Desktop.
Класс создает шорткод, и подгружает скрипты только там где выводится шорткод. #wordpress
This file contains 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 foobar_shortcode { | |
static $add_script; | |
static function init () { | |
add_shortcode('foobar', array(__CLASS__, 'foobar_func')); | |
add_action('init', array(__CLASS__, 'register_script')); | |
add_action('wp_footer', array(__CLASS__, 'print_script')); | |
} | |
static function foobar_func( $atts ) { | |
self::$add_script = true; | |
return "foo and bar"; | |
} | |
static function register_script() { | |
wp_register_script( 'foo-js', get_template_directory_uri() . '/includes/js/foo.js'); | |
} | |
static function print_script () { | |
if ( !self::$add_script ) return; | |
wp_print_scripts('foo-js'); | |
} | |
} | |
foobar_shortcode::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment