ThemeForest

Shortcode & Javascript

374 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 1-2 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.

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

2934 posts
  • Community Superstar
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Has been a member for 2-3 years
  • United States
  • 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

374 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 1-2 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?

2934 posts
  • Community Superstar
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Has been a member for 2-3 years
  • United States
  • 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

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

374 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 1-2 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?

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

374 posts
  • Bought between 50 and 99 items
  • Europe
  • Exclusive Author
  • Has been a member for 1-2 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