- Most Wanted Bounty Winner
- Sold between 250 000 and 1 000 000 dollars
- Has been a member for 5-6 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Won a Competition
- Bought between 100 and 499 items
- Exclusive Author
- Referred between 200 and 499 users

I am a bit new on these waters so i am constantly bumping into road blocks and this is one that i can’t pass at the moment.
I am working on a wp plugin and i want to use it as a shortcode. I have a script that i need to include in the header and when i use shortcodes with no custom parameters it works, but as soon as i put a parameter in the shortcode, the script doesn’t get included in the header anymore..
Does anyone know this error? Is there something that i am missing? This is my code:
<?php add_filter('the_posts', 'add_script_conditionally');
//search for the shortcode. if it exists, add the js
function add_script_conditionally($posts){
if (empty($posts)) return $posts;
$shortcode_found = false;
foreach ($posts as $post) {
if (stripos($post?>post_content, '[plugin_name]')) {
$shortcode_found = true;
break;
}
}
if ($shortcode_found) {
//enque main script
wp_enqueue_script('swfObject789', $home_url.'/wp-content/plugins/plugin_name/js/swfobject.js',false,null);
}
return $posts;
}
function add_slideshow($params=array()) {
echo 'test';
}
//register shortcode
add_shortcode('plugin_name', 'add_slideshow');
?>
When i use the shortcode like this, the script gets embedded in the header:
[plugin_name]However, when i use this code, it doesn’t work:
[plugin_name id="6"]
- Sold between 250 000 and 1 000 000 dollars
- Community Moderator
- Author was Featured
- Item was Featured
- Bought between 50 and 99 items
- Referred between 1000 and 1999 users
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
Here, you are searching specifically for [plugin_name]. When you add a parameter, your strings no longer match ([plugin_name] != [plugin_name ...] ).
if (stripos($post->post_content, '[plugin_name]')) {
$shortcode_found = true;
Here, you only enqueue the script if the string ‘[plugin_name]’ was found:
if ($shortcode_found) {
//enque main script
wp_enqueue_script('swfObject789', $home_url.'/wp-content/plugins/plugin_name/js/swfobject.js',false,null);
}
If you always want the script included, remove the conditional.
If you only want the script included when the shortcode has been used in the post content, just remove the closing ] from your stripos string – or if you want to be more precise use a regular expression (preg_match) to match [plugin_name .*].
Hope that helps 
- Most Wanted Bounty Winner
- Sold between 250 000 and 1 000 000 dollars
- Has been a member for 5-6 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Won a Competition
- Bought between 100 and 499 items
- Exclusive Author
- Referred between 200 and 499 users
- Sold between 250 000 and 1 000 000 dollars
- Community Moderator
- Author was Featured
- Item was Featured
- Bought between 50 and 99 items
- Referred between 1000 and 1999 users
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
Glad it helped you out – and don’t worry, once you spend a lot of time with things like this those types of issues will start to jump out at you 

