Skip to content

Instantly share code, notes, and snippets.

@organizm
Last active May 30, 2016 09:36
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 organizm/ede6ee70d1805426f4a0 to your computer and use it in GitHub Desktop.
Save organizm/ede6ee70d1805426f4a0 to your computer and use it in GitHub Desktop.
Класс создает шорткод, и подгружает скрипты только там где выводится шорткод. #wordpress
<?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