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


#1

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? :slight_smile:


#2

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). :slight_smile:


#3

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

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


#4

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.


#5

Yes, random numbers are good too.

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


#6

@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


#7
robot_operator said

@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

That wouldn’t work. The function stops executing after ‘return $my_shortcode_stuff;’. Do static $i = 0; $i++; at the top instead!


#8

Old thread, but here is what I think, increment-ing a value wouldn’t work as some of you suggested, as this value is not getting stored anywhere, right? So I think it is better to use some kind of Unix timestamp technique, like appending the shortcodes name with the timestamp value, like so:

$id = ‘tab’ . time();

And then ull have a unique id. And I believe there is no maximum limit on id or class names, so this should be fine. However, if you are adding more than 1 shortcode of the same type and at the same time, then you might need to increment the $id or change it some how so it is not the same for each shortcode you add.

This is what I’ve come up with, I’m also not sure how it is done by other developers