come with an interesting conundrum. I have to load 8 javascript files and also the same quantity of styles for my wordpress plugin. They are only needed anywhere my shortcode is went.

I have attempted to load all of them with print_styles and print_scripts however they aren't rendering correctly, plus to do this breaks xhtml validation. So right now they strain on every page and because of the amount of files needed it is not achievable to depart it such as this.

On another project I authored a function into my plugin's index.php file that will go ahead and take current page, search it for my shortcode and when found only then wouldn't it print the scripts, but it is really an ugly hack.

Has anybody got any suggestions or solutions? any help could be appreciated, regards, Daithi

BraedenP is appropriate, I am confident there's not a way to identify shortcode usage in the execution duration of wordpress_enqueue_scripts / once the stylesheets load.

Can there be any reason you have to do this in 8 files? You might you need to be more effective, then it might not be considered a problem to load it on every page.

You could look at a PHP stylesheet solution that only executes certain styles as needed. A css.php file look like:

<?php
header("content-type: text/css");
/* You can require the blog header to refer to WP variables and make queries */
//require '../../../wp-blog-header.php';
$css = '';
$css .= file_get_contents('style.css');
/* Consider using GET variables or querying a variable in the WP database to determine which stylesheets should be loaded. You could add an option to the backend that allows a stylesheet to be turned on or off. */
if($condition1 == TRUE) $css .= file_get_contents('condition1.css');
if($condition2 == TRUE) $css .= file_get_contents('condition2.css');
?>

Less scripts and fewer stylesheets means less http demands along with a faster load time.

The number of pages are these scripts likely to be loaded on? Will it be achievable to keep a range of pages, and just load the scripts/stylesheets once the current page is incorporated in the array?

Otherwise, without checking the code there's no method of doing this, as Wordpress does not know the shortcode is available until well in to the page load.

to reply to my very own question... I'd it write the very first time. You need to search each page to check on that the shortcode has been used. This needs to be achieved when page information is loaded and before page is displayed. In my experience it's complete overkill around the system, but regrettably it's the way it's. I acquired these details from: get_shortcode_regex and old nabble

First:

add_action('template_redirect','wp_my_shortcode_head');

then:

function wp_my_shortcode_head(){
  global $posts;
  $pattern = get_shortcode_regex(); 
  preg_match('/'.$pattern.'/s', $posts[0]->post_content, $matches); 
  if (is_array($matches) && $matches[2] == 'YOURSHORTCODE') { 
        //shortcode is being used 
  }
}

replace 'YOURSHORTCODE' using the title of the shortcode and add your wordpress_enqueue_scripts into where it states //shortcode has been used.