3448 posts Ruben Bristian
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 500 and 999 users
+5 more
KrownThemes says
Hi guys! :)
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"]
3424 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • United States
  • Has been a member for 4-5 years
+5 more
sevenspark Volunteer moderator says

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 :)

3448 posts Ruben Bristian
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 500 and 999 users
+5 more
KrownThemes says

Hope that helps :)
Worked like a charm…thanks! :)

Gosh, i have so much to learn :D
3424 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • United States
  • Has been a member for 4-5 years
+5 more
sevenspark Volunteer moderator says

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 :)

by
by
by
by
by
by