Created
May 16, 2019 14:52
-
-
Save ThatGuySam/7363b2e07a39828b9ea74c3efd7965e4 to your computer and use it in GitHub Desktop.
Wordpress Shortcode Class
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 Cool_Shortcode_Name { | |
static $add_script; | |
static function init() { | |
add_shortcode('cool_shortcode_name', array(__CLASS__, 'handle_shortcode')); | |
add_action('init', array(__CLASS__, 'register_script')); | |
add_action('wp_footer', array(__CLASS__, 'print_script')); | |
add_action('wp_footer', array(__CLASS__, 'internal_script') ); | |
} | |
static function handle_shortcode($raw_atts) { | |
self::$add_script = true; | |
$atts = shortcode_atts( array( | |
'argument' => 'default', | |
), $raw_atts, 'cool_shortcode_name' ); | |
ob_start(); | |
?> | |
<div> | |
Template! | |
</div> | |
<?php | |
$content = ob_get_clean(); | |
return $content; | |
} | |
static function register_script() { | |
//wp_register_style( 'font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css', array(), '4.1.0', 'screen' ); | |
//wp_register_script('imagesloaded', '//cdnjs.cloudflare.com/ajax/libs/jquery.imagesloaded/3.0.4/jquery.imagesloaded.js', array('jquery'), '3.0.4', true); | |
} | |
static function print_script() { | |
if ( ! self::$add_script ) | |
return; | |
//wp_print_styles('font-awesome'); | |
//wp_print_scripts('imagesloaded'); | |
} | |
static function internal_script() { | |
if ( ! self::$add_script ) | |
return; | |
?> | |
<script type="text/javascript"> | |
// Javascript goes here | |
</script> | |
<style> | |
/* CSS Goes Here */ | |
</style> | |
<?php | |
} | |
} | |
Cool_Shortcode_Name::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment