1

I am trying to insert WordPress shortcode into JS but haven't managed so far. I found some jQuery code that I needed to translate into vanilla JS and I am pretty sure I did not do the right thing since it's not working. I do not fully understand the code I came across, which means I need some help understanding where I've gone wrong and why my shortcode isn't showing on my site. Maybe the PHP code is not correctly linked to the JS file, I'm not too sure.

The goal is to have the shortcode (a WP form in this case) show in the modal when it's clicked on. Any help is greatly appreciated!

Here is the jQuery code (AJAX request):

        $.ajax({
          url: `<?php echo admin_url('admin-ajax.php'); ?>`,
          type: "GET",
          data: {
            action: "runThisPhpFunction",
          },
          success: function (data) {
            $("#trigger_target").html(data);
          },
        });

And here is my JS code for the modal, into which I am trying to place the shortcode:

function newsletterModal() {
  const ajaxurl = `<?php echo admin_url('admin-ajax.php'); ?>`;

  const btn = document.querySelector("#menu-item-2745");
  btn.setAttribute("id", "trigger_my_shortcode");
  const modal = document.createElement("div");
  modal.setAttribute("id", "trigger_target");
  modal.classList.add("modal");

  modal.innerHTML = `<div class="modal-content">
    <span class="close">&times;</span>
    <h2>Sign up to our newsletter</h2>
    <h5>And get hold of your 10% discount!</h5>
    <p>insert [wpforms id="1703"] here</p>
    </div>`;
    
    btn.onclick = function (e) {
    e.preventDefault();
    modal.style.display = "block";

    document
      .querySelector("#trigger_my_shortcode")
      .addEventListener("click", (e) => {
        e.preventDefault();

          fetch(ajaxurl, {
          type: "GET",
          data: {
            action: "runThisPhpFunction",
          },
          success: function (data) {
            document.querySelector("#trigger_target").html(data);
          },
        });
      });
  };

  modal.querySelector(".close").onclick = function () {
    modal.style.display = "none";
  };
  window.onclick = function (e) {
    if (e.target == modal) {
      modal.style.display = "none";
    }
    document.body.appendChild(modal);
  };
}

And the functions.php code:

function runThisPhpFunction() {
    if ( isset($_REQUEST) ) {
    
      echo do_shortcode( '[wpforms id="1703"]' );
  
    }
    die();
  }
  add_action( 'wp_ajax_runThisPhpFunction', 'runThisPhpFunction' );
  add_action( 'wp_ajax_nopriv_runThisPhpFunction', 'runThisPhpFunction' );

1 Answers1

2

There is quite a lot going on with your code, so the answer won't be short. It's generally a good practice to split your code up into smaller functions which all have their specific purpose. So I've taken the liberty to rewrite some functions so that they will help you in getting where you need to go.

The code below works as followed: The buildModal function builds all the HTML for your modal and can take a form as an argument. That form should be text because it needs to be interpolated (combined) with your other elements in the same string.

The buildModal function will be called from the getFormAndBuildModal function. The getFormAndBuildModal function uses fetch to send a request to the server and interprets the response as text. This text is your form, which will be passed to the buildModal to build the modal with the form in it.

The button with the #menu-item-2745 will be the trigger to send the request and build the form.

Working this way means that every time that you would click on your button it would call the server, build a new modal and show it on the page. Then when closing the modal, it removes the modal from the page.

I've tried to explain as much as possible of what's happening in the code and what each step is doing. If some things are still unclear, please let me know and I'll try to clear it up.

function buildModal(form) {
  const modal = document.createElement("div");
  modal.id = "trigger_target"
  modal.classList.add("modal");

  /**
   * Create close button here so we can attach the 
   * event listener without having to select it later.
   */
  const modalClose = document.createElement("span");
  modalClose.classList.add("close");
  modalClose.addEventListener("click", function() {

    /**
     * Remove the modal completely from the document.
     * This isn't mandatory and you can change it if you'd like.
     */
    modal.remove();
  });

  const modalContent = document.createElement("div");
  modalContent.classList.add("modal-content");

  /**
   * The form will be a string of HTML that gets injected
   * into another string of HTML here below. The innerHTML setter
   * will then parse the entire string, with form, to HTML.
   */
  modalContent.innerHTML = `
    <div class="modal-content">
      <h2>Sign up to our newsletter</h2>
      <h5>And get hold of your 10% discount!</h5>
      <p>${form}</p>
    </div>`;

  /**
   * First append the close button, then the content.
   */
  modal.append(modalClose, modalContent);

  /**
   * Return the HTML modal element.
   */
  return modal;
}

Here I've added how to use PHP in JavaScript and a way to tackle the issue with selecting the button. There are two solutions to this problem, one is here at the second comment and the other solution is in the PHP snippet after this one.

/**
 * Check the PHP snippet at the bottom how this gets here.
 * This is the result of the array turned into JSON and then
 * placed into the document with wp_add_inline_script. 
 *
 * Sidenote: __wp__ looks ugly, but it will make sure that if 
 * a browser might get updated and a new property is added to the
 * window object, it will never overwrite or break anything because
 * the name is so unique.
 */
const ajaxurl = __wp__.ajax;

/**
 * Select the button and listen for the click event.
 * When clicked, fire the getFormAndBuildModal function.
 *
 * Update: with selecting elements it is paramount that the element is
 * above the <script> tag in the document. 
 * Otherwise the element would not yet exist and the result would come up empty.
 * Another way is to wait for the document to give a signal when every element has been rendered with the DOMContentLoaded event.
 */
// document.addEventListener('DOMContentLoaded', function(event) {
//  const button = document.querySelector("#menu-item-2745");
//  button.addEventListener("click", getFormAndBuildModal);
// });

const button = document.querySelector("#menu-item-2745");
button.addEventListener("click", function(event) {
  event.preventDefault();
  getFormAndBuildModal();
});

function getFormAndBuildModal() {
  /**
   * Fetch uses the GET method by default.
   * All you need to do is to add the action to the URL
   * so that WP knows what action to call on the server.
   */
  fetch(`${ajaxurl}?action=runThisPhpFunction`)

  /**
   * Fetch can take while to load, so it uses a promise.
   * With .then() we say what happens after fetch is finished.
   * Fetch gives us a response object as a result, which we need to inspect.
   */
  .then(response => {

    /**
     * If the response has gone wrong, show an error.
     */
    if (!response.ok) {
      throw new Error("runThisPhpFunction request has failed");
    }

    /**
     * Otherwise we use the content that the response has send us.
     * Currently the "body" (your form) of the response is just bits and bytes.
     * We can tell the response how we want to use the response.
     * With the .text() method we turn the raw data into a string.
     * That string can later be used as HTML. :)
     */
    return response.text();
  })

  /**
   * response.text() also returns a promise, just like fetch. So to go to the next step
   * we use another .then() function. In here we have our form in a string.
   * Now we can build the modal and pass the form as an argument. The modal 
   * will be build and the form turned into HTML and put in the correct position.
   * When the buildModal function is done it returns the result.
   * Now append it to the body and it's done.
   */ 
  .then(form => {
    const modal = buildModal(form);
    document.body.append(modal);
  });
}

Here I've added a couple more additions to enqueueing the script and how to turn PHP into JavaScript the right way. ;)

function enqueue_my_custom_script() {
  /**
   * Instead of printing PHP variables directly inside JavaScript, 
   * you could use this method to let PHP do that for you. 
   * The array here below we be turned into a JSON string, 
   * which will later be turned into a JavaScript object that you
   * can use in your main script.
   */
  $wp_js_data = json_encode( 
    array(
      'ajax' => admin_url( 'admin-ajax.php' ),
    )
  );

  /**
   * The last parameter of wp_register_script (there are 5) will
   * determine if the script will be placed in the <head> tag when 
   * the value is false, or before the end of the </body> tag when
   * the value is true. The latter will make sure that your JS executes
   * AFTER all other HTML elements have been rendered. With this you don't
   * have to listen for the DOMContentLoaded event in JavaScript.
   *
   * Use get_stylesheet_directory_uri() to get the path to your child
   * theme directory instead of hard linking to your sheet. This will
   * output the URL to the directory of your style.css file of your theme.
   */
  wp_register_script( "scriptjs", get_stylesheet_directory_uri() . "/script.js", array(), null, true );

  /**
   * Here we create a global variable on the window object. This makes
   * the data is available in every JavaScript file. Here we insert the JSON string
   * that will be turned into a usable JS object.
   *
   * Be sure to output this script BEFORE the "scriptjs" file.
   */
  wp_add_inline_script( "scriptjs", "window.__wp__ = {$wp_js_data}", "before" );

  /**
   * This used to be the first line. wp_register_script only registers
   * the script but does not output it. This enables you to do something
   * like wp_add_inline_script before outputting the script.
   * wp_enqueue_script makes sure that the registered script will be 
   * placed in the document.
   */
  wp_enqueue_script( "scriptjs" );
} 
add_action( "wp_enqueue_scripts", "enqueue_my_custom_script" );
Dharman
  • 30,962
  • 25
  • 85
  • 135
Emiel Zuurbier
  • 19,095
  • 3
  • 17
  • 32
  • First of all, thank you, I really appreciate you taking the time to help me. I definitely have a better understanding of it and I can see why it is more useful to split up the function so that each function serves a specific purpose. However, I tried implementing your code and I get an error when it comes to the button. Somehow it returns null in the console and shows me this error (Uncaught TypeError: Cannot read property 'addEventListener' of null). The weird thing is that I can't see what's wrong with it, because the button variable is assigned to the correct link id. – Mégane Londoño Oct 07 '20 at 12:26
  • You're welcome and I'm glad that I could help you understand. The result from `document.querySelector("#menu-item-2745")` appears to return `null`. That means that the element has not been found in the document. This happens when JavaScript is executed before the button has been rendered on the page, or when the button does not exist on the page where you run your JavaScript. How are you adding the script to your page? – Emiel Zuurbier Oct 07 '20 at 15:18
  • I'm linking it through PHP like this: `function enqueue_my_custom_script() { wp_enqueue_script("scriptjs", "https://martadolska.com/wp-content/themes/astra-child/script.js" , false); } add_action("wp_enqueue_scripts", "enqueue_my_custom_script");` – Mégane Londoño Oct 07 '20 at 16:20
  • Everything else has worked so far though, so I am not sure there is an issue with how I am linking it... – Mégane Londoño Oct 07 '20 at 16:21
  • I've added some more comments and code for you to read. The `wp_enqueue_script` part was almost perfect, but there was something missing. The third snippet will hopefully explain what you could do to control the placement of your ` – Emiel Zuurbier Oct 07 '20 at 16:58
  • Great, thank you. So that means I can now replace the inline PHP I had in my script with `const ajaxurl = "admin-ajax.php"`? – Mégane Londoño Oct 07 '20 at 17:13
  • Not quite. PHP will output a script that creates a property on the `window` object called `__wp__`. This property will also be an object with the `ajax` property. That has the ajax url as value. So it the line will be `const ajaxurl = window.__wp__.ajax` or `const ajaxurl = __wp__.ajax` as the the `window` is implied in the current scope. I've added this example in the second snippet. – Emiel Zuurbier Oct 07 '20 at 17:38
  • Ok, that makes more sense. I've implemented that now and getting an error saying that `_wp_ is not defined` though... – Mégane Londoño Oct 07 '20 at 18:48
  • It's with a double underscore. Make sure that the `window.__wp__` or `__wp__` in your script is the same as in `wp_add_inline_script( "scriptjs", "window.__wp__ = {$wp_js_data}" );` – Emiel Zuurbier Oct 07 '20 at 19:32
  • Yes, sorry, i forgot to include the double underscore here, but it was present in my code from the beginning. This is how it's looking in PHP: `wp_add_inline_script( "scriptjs", "window.__wp__ = {$wp_js_data}" );`, and in JS: `const ajaxurl = window.__wp__.ajax;`. And `window.__wp` is still showing up as undefined for some reason. – Mégane Londoño Oct 07 '20 at 20:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222684/discussion-between-emiel-zuurbier-and-meganelondono). – Emiel Zuurbier Oct 07 '20 at 20:11