3711 posts Ruben Bristian
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+10 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"]
3503 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark 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 :)

3711 posts Ruben Bristian
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+10 more
KrownThemes says

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

Gosh, i have so much to learn :D
3503 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark 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 :)

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by