572 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $250,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Had an item featured 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’);

572 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $250,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Had an item featured 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.

51 posts AA-Team
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $250,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
+5 more
AA-Team says

nop, it’s ok

572 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $250,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
+6 more
radykal says

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

3497 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

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')) {
572 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $250,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
+6 more
radykal says

Yes you are right. Thanks

1693 posts Building the Web.
  • Has been a beta tester for an Envato feature
  • Sells items exclusively on Envato Market
  • Has sold $250,000+ on Envato Market
  • Had an item featured 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.

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