$shortcode = new Shortcode_Class();
class Shortcode_Class
{
/**
* If you should add the script or not
*
* @var bool
*/
private $addScript = false;
public function __construct()
{
add_shortcode('test_shortcode', array($this, 'shortcode_content'));
// wp_enqueue_scripts
// If you use the below the CSS and JS file will be added on everypage
// add_action( 'wp_enqueue_scripts', array($this, 'add_shortcode_scripts'));
// Add styles and scripts to the page
add_action('wp_footer', array($this, 'add_shortcode_scripts'));
}
public function shortcode_content( $attr, $content )
{
$this->addScript = true;
?>
<h1>Shortcode Content Being Displayed</h1>
<?php
}
public function add_shortcode_scripts()
{
if(!$this->addScript)
{
return false;
}
wp_enqueue_script('shortcode-js', get_stylesheet_directory_uri() . '/js/shortcode.js', false);
}
}
This helped!