Using javascript in Shortcode Callback when creating new plugin
-
Hi everyone, I hope I can get some help with this. I am developing a new plugin and have created it inside a shortcode function. Inside this function, I am also using some javascript. When I use the shortcode on a page, it works no problem, but if I try run it twice, the second instance gives me an error that my let variable inside my javascript has already been declared.
So I could move the javascript to an external file using enqueue so it only calls it once, but the javascript doesn’t work unless I am inside the shortcode callback function. I know it is because I have saved a shortcode attribute into a variable called $time, and this $time variable basically populates the entire javascript block.
I don’t know how to solve this so that I can have multiple shortcodes on one page, and not have a javascript variable redeclaration problem.
Here is my code:
function devgirl_countdown_clock_function($atts = '', $content = null) { $value = shortcode_atts( array( 'time' => "5 December 2023 8:00", 'end-notice' => "Countdown Ended", 'clock-colour' => "#011c25", 'text-colour' => "DarkTurquoise", ), $atts, 'devgirl_countdown_clock_function' ); $time = sanitize_text_field($value['time']); $end_notice = sanitize_text_field($value['end-notice']); $clock_colour = sanitize_text_field($value['clock-colour']); $text_colour = sanitize_text_field($value['text-colour']); ?> <h2 class="end-notice"></h2> <div class="devgirl-countdown-clock border-radius"> <?php if ( ! function_exists( 'countdownBlocks' ) ) { function countdownBlocks($clock_colour, $text_colour, $date_value, $date_value_title) { ?> <div class="countdown-block" style="background:<?php echo esc_html($clock_colour); ?>; color:<?php echo esc_html($text_colour); ?>"> <div class="clock <?php echo esc_html($date_value); ?>-clock-value"></div> <div class="text"><?php echo esc_html($date_value_title); ?></div> </div><!---countdown-block--> <div class="separators">:</div> <?php } countdownBlocks($clock_colour, $text_colour, 'days', 'Days'); countdownBlocks($clock_colour, $text_colour, 'hours', 'Hours'); countdownBlocks($clock_colour, $text_colour, 'mins', 'Mins'); countdownBlocks($clock_colour, $text_colour, 'secs', 'Secs'); } else { echo 'The function name already exists and cannot be run. Plese check all your plugins function names to find the conflict.'; } ?> </div><!---devgirl-countdown-clock--> <script type="text/javascript"> // Set the date we're counting down to const countDownDate = new Date('<?php echo $time; ?>').getTime(); // Update the count down every 1 second let x = setInterval(function() { // Get today's date and time let now = new Date().getTime(); // Find the distance between now and the count down date let distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds let days = Math.floor(distance / (1000 * 60 * 60 * 24)); let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); let seconds = Math.floor((distance % (1000 * 60)) / 1000); // Output the result in an element with id="demo" document.querySelector('.devgirl-countdown-clock').style.display = 'flex'; document.querySelector('.days-clock-value').innerHTML = days; document.querySelector('.hours-clock-value').innerHTML = hours; document.querySelector('.mins-clock-value').innerHTML = minutes; document.querySelector('.secs-clock-value').innerHTML = seconds; // If the count down is over, write some text if (distance < 0) { clearInterval(x); document.querySelector("h2.end-notice").textContent = '<?php echo $end_notice; ?>'; document.querySelector('.devgirl-countdown-clock').style.display = 'none'; } }, 1000); </script> <?php } //END devgirl_countdown_clock_function add_shortcode('devgirl-countdown-clock', 'devgirl_countdown_clock_function'); // front end stylesheet function devgirl_countdown_clock_frontend_style() { wp_enqueue_style( 'countdown-clock-frontend', plugins_url( '/style/countdown-clock-frontend.css' , __FILE__ ) ); } add_action( 'wp_enqueue_scripts', 'devgirl_countdown_clock_frontend_style' ); // backend admin stylesheet function devgirl_countdown_clock_backend_style() { wp_enqueue_style( 'countdown-clock-backend', plugins_url( '/style/countdown-clock-backend.css' , __FILE__ ) ); } add_action( 'admin_enqueue_scripts', 'devgirl_countdown_clock_backend_style' ); ?>
And then the user can call the shortcode in WordPress using something as simple as this:
[devgirl-countdown-clock time="3 Jul 2023 8:00"]
- The topic ‘Using javascript in Shortcode Callback when creating new plugin’ is closed to new replies.