0

I made an ajax call to a php function, that should render a shortcode:

public function loadShortCode(){
    $shortcode = do_shortcode('[elementor-template id="2635"]', false );
    wp_send_json($shortcode);
}

Sadly it just returns it as text "[elementor-template id="2635"]". Is it possible to get the html from the shortcode and print it somewhere via JS? It is being done in a .js file, so I can't use php echo manipulation.

I already tried this, but this just returns the shortcode as text in my html

const loadShortCode = async () => {
const response = await fetch(wp.ajax_url + "?action=loadShortCode", {
    method: "POST",
});

return response.json();

}

(async() => {
    const response = await loadShortCode();
    console.log(response);
    membershipTypesContainer.innerHTML = response;
})();
 
add_action('wp_ajax_nopriv_loadShortCode', [__CLASS__, 'loadShortCode']);
        add_action('wp_ajax_loadShortCode', [__CLASS__, 'loadShortCode']);
7
  • How are you making the AJAX request? And how are you registering the callback? Are you using admin-ajax.php or the REST API? Jan 10 at 13:31
  • add_action('wp_ajax_nopriv_loadShortCode', [CLASS, 'loadShortCode']); add_action('wp_ajax_loadShortCode', [CLASS, 'loadShortCode']); and const loadShortCode = async () => { const response = await fetch(wp.ajax_url + "?action=loadShortCode", { method: "POST", }); return response.json(); } (async() => { const response = await loadShortCode(); console.log(response); membershipTypesContainer.innerHTML = response; })();
    – Jan
    Jan 10 at 14:00
  • pastebin.com/NcHE472S
    – Jan
    Jan 10 at 14:01
  • 1
    Please just add any additional code to the question. Jan 10 at 14:45
  • 1
    My guess is that the shortcode elementor-template isn't registered during AJAX calls. (or you're using an invalid shortcode).
    – Paul G.
    Jan 11 at 14:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.