- Author had a File in an Envato Bundle
- Author was Featured
- Bought between 50 and 99 items
- Exclusive Author
- Has been a member for 4-5 years
- Item was Featured
- Referred between 500 and 999 users
- Sold between 250 000 and 1 000 000 dollars
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? 
- Author had a File in an Envato Bundle
- Sold between 250 000 and 1 000 000 dollars
- Author was Featured
- Item was Featured
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 3-4 years
- India
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). 
You can have incremented ID just by using static variable to hold previous value.
Details: http://php.net/manual/en/language.variables.scope.php
- Author had a File in an Envato Bundle
- Author was Featured
- Bought between 50 and 99 items
- Exclusive Author
- Has been a member for 4-5 years
- Item was Featured
- Referred between 500 and 999 users
- Sold between 250 000 and 1 000 000 dollars
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.
Yes, random numbers are good too.
The only advantage of using incremented ID: you’ve got exactly same value after every page refresh:)
@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 