PhotoDune

Unique ID for WordPress shortcodes when used more than once on a page?

1025 posts
  • Author had a File in an Envato Bundle
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 500 and 999 users
  • Sold between 250 000 and 1 000 000 dollars
+1 more
ThemeBlvd says

I’m currently coding a WordPress shortcode and it has some javascript involved. For javascript to work properly I need to use a div with a unique #ID. If the shortcode is used once, it works fine, but if they were to use the shortcode more than once on a page, it would break the javascript.

So, I’m wondering if there is some way to use a unique ID every time the shortcode is called? Or some way to have a different ID if the shortcode is used more than once on a page?

Currently, I have an attribute that’s an ID, and I have to explain, “okay, if you use it more than once on a page, you have to assign a unique ID”... I’d like to avoid doing this.

I did see in someone’s source code one time they had like a %& funky thing that looked like it was doing this. Lol but I can’t remember how they did it, and I can’t think anything to Google that will give what Im looking for.

Any suggestions? :-)

461 posts
  • Author had a File in an Envato Bundle
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Has been a member for 3-4 years
  • India
+1 more
SaurabhSharma says

What you have done so far is more than good. :). There is no harm or inconvenience in asking for an ID as attribute. There are ways, like generating random string using php logic, or appending a string with random integers. But this technique is not full proof. Even randomly generated strings may be of same nature (1 out of 100 chance). :)

85 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Europe
  • Referred between 1 and 9 users
bringthepixel says

You can have incremented ID just by using static variable to hold previous value.

Details: http://php.net/manual/en/language.variables.scope.php

1025 posts
  • Author had a File in an Envato Bundle
  • Bought between 50 and 99 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 500 and 999 users
  • Sold between 250 000 and 1 000 000 dollars
+1 more
ThemeBlvd says

Thanks for your suggestions, guys. I ended up just having a random number generated. For the ID each time. The number is actually between 1-10,000 I think lol. I figured the chances were real slim that they’d ever be the same.

85 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Europe
  • Referred between 1 and 9 users
bringthepixel says

Yes, random numbers are good too.

The only advantage of using incremented ID: you’ve got exactly same value after every page refresh:)

1 post
  • Has been a member for 2-3 years
robot_operator says

@bringthepixel has it right, you need to use a static variable to increment it properly. I just built a shortcode this way. Something like this:

function my_shortcode() { STATIC $i = 1; $my_shortcode_stuff='<div id="div'.$i.'">My content</div>'; return $my_shortcode_stuff; $i++; } //end function
by
by
by
by
by