566 posts
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
radykal says

Hey guys,

currently I´m building my first wp plugin and I´m searching for a common solution to add javascript/sytles only when the shortcode is set in a post. I already found a script which helps me:

add_filter('the_posts', 'conditionally_add_scripts_and_styles');

function conditionally_add_scripts_and_styles($posts){
    if (empty($posts)) return $posts;

    $shortcode_found = false; // use this flag to see if styles and scripts need to be enqueued
    foreach ($posts as $post) {
        if (stripos($post->post_content, '[shortcodeName]')) {
            $shortcode_found = true; // bingo!
            break;
        }
    }

    if ($shortcode_found) {        
        // enqueue here
        wp_enqueue_style();
        wp_enqueue_script();

    }

    return $posts;
}

It works, but now I have another issue. I would also like to add javscript block which set some options of the jquery plugin I´m using. I´m doing that for it:

function addSC() {    
    return  "<script type="text/javascript">
        $.jqueryPlugin.option = 'blabla';
        </script>
        <div class="anyClass">..</div>";
}

add_shortcode('shortCodeName', 'addSC');

But now the console tells me the ’$ is undefinied’. So I think that the addSC() function gets added first and then the scrips and sytles will be added. Anybody have a common solution for that problem? Any other techniques are welcome….. :)

72 posts
  • Has been part of the Envato Community for over 3 years
  • Has referred 100+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+4 more
yougapi says

For the undefined ”$”, if you are using jQuery just replace it by “jQuery”. Ex: jQuery(this).(‘blabla’);

566 posts
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
radykal says

Oh yes. Thanks. This is working. If anybody has some better solution than mine, please let me know.

50 posts AA-Team
  • Has referred 50+ members
  • Has sold $250,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+6 more
AA-Team says

nop, it’s ok

566 posts
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
radykal says

ok great. Does anybody know good blogs/sources for wordpress plugin and theme development???

3431 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark Volunteer moderator says

Just a quick suggestion – I might remove the trailing square bracket from this line:

if (stripos($post->post_content, '[shortcodeName]')) {

Otherwise, you won’t find any shortcodes that have attributes set. e.g., I don’t think that will match

[shortcode att1="one" att2="two"]

This might be better:

if (stripos($post->post_content, '[shortcodeName')) {
566 posts
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
radykal says

Yes you are right. Thanks

1630 posts Building the Web.
  • Has referred 50+ members
  • Has sold $250,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+7 more
ZoomIt says

Are you sure the ‘the_posts’ filter is the best in this case ?

I use wp_print_styles and works great.

by
by
by
by
by
by