Iâm currently coding a WordPress shortcode and it has some jquery involved. For jquery 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?
Suppose i have the shortcode function
function my_shortcode() {
$my_shortcode='<div id="demo">My content</div>';
return $my_shortcode;
} //end function
and jquery
$("#demo").click(function{
autoplay: true,
});
You can use a static variable
and use it as part of the ID
You are free to set the shortcodeâs id among with other parameters:
I would recommend using an UUID.
In my projects I generate it and attach it the the
$atts
element.This can be easily attached to a data attribute, e.g.