0

How to trigger php code, in JavaScript if condition? I need to start some shortocode in WordPress if the condition is yes.

I have this code. Is it ok?

<script src="https://ssp.seznam.cz/js/ssp.js"></script>
<script>

  if (sssp.displaySeznamAds()) {
    // display ads
    <?php echo do_shortcode('[seznam-ads id="164107"]'); ?>
  } else {
    // another ads
document.write('do 30min');
  }
</script>
j08691
  • 204,283
  • 31
  • 260
  • 272
Pavel H.
  • 1
  • 1
  • 1
    By the time your script runs, the PHP has already executed. You'll likely have to make an AJAX call to run your PHP. – mykaf Sep 06 '22 at 19:06

1 Answers1

3

PHP is entirely server side. All of the PHP code written to display your page is run once per page-load, and that's it. You cannot directly trigger any more PHP code execution after the browser has received the page.

If you need to update your page in real time with some data presented to you from PHP, you can do that with a method called "AJAX", which initiates a new request to a page from JavaScript, allowing you to retrieve the results of that page request in your script and do what you like with it inside of your current page.

Here's an introduction to AJAX that will get you started: W3 Schools AJAX Introduction

Alex
  • 310
  • 1
  • 6