439 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 5 000 and 10 000 dollars
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.

3058 posts
  • Community Superstar
  • Has been a member for 6-7 years
  • Won a Competition
  • Sold between 50 000 and 100 000 dollars
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Exclusive Author
+1 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”....

3114 posts
  • Sold between 5 000 and 10 000 dollars
  • United States
  • Bought between 10 and 49 items
  • Has been a member for 3-4 years
  • Exclusive Author
chrisakelley 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

439 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 5 000 and 10 000 dollars
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?

3114 posts
  • Sold between 5 000 and 10 000 dollars
  • United States
  • Bought between 10 and 49 items
  • Has been a member for 3-4 years
  • Exclusive Author
chrisakelley 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

3058 posts
  • Community Superstar
  • Has been a member for 6-7 years
  • Won a Competition
  • Sold between 50 000 and 100 000 dollars
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Exclusive Author
+1 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.

439 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 5 000 and 10 000 dollars
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?

3058 posts
  • Community Superstar
  • Has been a member for 6-7 years
  • Won a Competition
  • Sold between 50 000 and 100 000 dollars
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Exclusive Author
+1 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…

439 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 5 000 and 10 000 dollars
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.

by
by
by
by
by
by