438 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $5,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
Pixelous says

Hello, I have a shortcode which should output a javascript:

function my_shortcode( $atts, $content = null ) {    
        extract( shortcode_atts( array(
        ), $atts) );
$output = '';
$output .= '// javascript with php variables goes gere';
return $output;
}
add_shortcode('my_shortcode', 'index_my_shortcode');

As u can see, my shortcode outputs a javascript with php variables, but I am interesting is there a way to output this javascript under the footer? I know there is a way to serialise script but at current moment I am interesting to place javascript under the footer. As u know there is a wp_footer function http://codex.wordpress.org/Function_Reference/wp_footer But it’s possible to use it to place my javascript with shortcode’s php variables? Hope to hear a suggestions from someone, thanks.

3072 posts
  • Has referred 50+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Had an item featured on Envato Market
+5 more
wickedpixel says

I would manage this thing in another way. Don’t write javascript with the shortcode, instead write the data you need in the tag attributes or as hidden tags. Later, the javascript that starts in the footer will use that content to do stuff needed.
For simple values use the html tag attribute “data-settingname”....

3436 posts
  • Has sold $5,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in United States
  • Has been part of the Envato Community for over 4 years
+1 more
organicbee says

I would manage this thing in another way. Don’t write javascript with the shortcode, instead write the data you need in the tag attributes or as hidden tags. Later, the javascript that starts in the footer will use that content to do stuff needed.
For simple values use the html tag attribute “data-settingname”....

cough http://codex.wordpress.org/Function_Reference/wp_localize_script

then use a meta-box/options panel to control it

438 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $5,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
Pixelous says

I would manage this thing in another way. Don’t write javascript with the shortcode, instead write the data you need in the tag attributes or as hidden tags. Later, the javascript that starts in the footer will use that content to do stuff needed.
For simple values use the html tag attribute “data-settingname”....

wickedpixel, could u provide an example, please or u, Chris?

3436 posts
  • Has sold $5,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in United States
  • Has been part of the Envato Community for over 4 years
+1 more
organicbee says


I would manage this thing in another way. Don’t write javascript with the shortcode, instead write the data you need in the tag attributes or as hidden tags. Later, the javascript that starts in the footer will use that content to do stuff needed.
For simple values use the html tag attribute “data-settingname”....
wickedpixel, could u provide an example, please or u, Chris?

what exactly are you trying to do? post the full code/explain it a bit

3072 posts
  • Has referred 50+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Had an item featured on Envato Market
+5 more
wickedpixel says

here: http://pastie.org/4344420
Anyway, this method works with scripts that start on window ready event too… or with footer, etc.

438 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $5,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
Pixelous says

here: http://pastie.org/4344420

Thanks for your reply, seems interesting solution, if I have several div id=”cool-viewer-’. $id .’” ? U know, div id=”cool-viewer-1”, div id=”cool-viewer-2”, div id=”cool-viewer-3”, etc. Should I use jquery .each function?

3072 posts
  • Has referred 50+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Had an item featured on Envato Market
+5 more
wickedpixel says

sure. You don’t even need unique id’s… you can use a class instead and use the .each on that class…

438 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $5,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
Pixelous says



I would manage this thing in another way. Don’t write javascript with the shortcode, instead write the data you need in the tag attributes or as hidden tags. Later, the javascript that starts in the footer will use that content to do stuff needed.
For simple values use the html tag attribute “data-settingname”....
wickedpixel, could u provide an example, please or u, Chris?
what exactly are you trying to do? post the full code/explain it a bit

Chris, actually a code big enought.

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