2

I have a <div id="floorplan"></div> Im using wordpress so in the javascript panel on clicking a button i have added the code

var code = "[insert_php] echo do_shortcode('[flat9]'); [/insert_php]";
        document.getElementById("floorplan").innerHTML = "PHP"+code;

but the whole "[insert_php] echo do_shortcode('[flat9]'); [/insert_php]" is printing without executing

i tried using this [insert_php], because if i use tags while printing result will be like "<!--?php echo do_shortcode('[flat9]'); --?> ,it will get commented because of this " innerHTML ". I need to print the short code in such a way, a real help on this would be appreciated.

prasanth
  • 22,145
  • 4
  • 29
  • 53
  • 1
    This is because you're adding the wordpress shortcode with javascript to the DOM in the client's browser, therefor it is not executed by PHP (PHP execution happends before the page is sent to the user, it's server side). I think the best way to solve this is using a ajax call to an endpoint which will generate the shortcode html code, when you receive the html code from the endpoint by using ajax you can then add it to your div. See https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_nopriv_(action) for more information on how to create an endpoint. – Jeroen Groenveld Mar 13 '19 at 10:08
  • 1
    you are trying to execute php from js , that is impossible , you can do that in another way , like putting that result of **echo do_shortcode('[flat9]');** in a hidden html tag and on click button , get it and concatenate it using js – Yassine CHABLI Mar 13 '19 at 10:10

2 Answers2

1

This is not possible. However you can do 2 things

  1. If it is a PHP file and you are writing JS with script tag then you can use PHP tags inside the script tags.

  2. Second option is to use WP JS Template ( Driven by Underscore.js ). With this you can send back PHP values converting in JS Objects and then you can use those values in to your HTML template of the fly.

Robi
  • 158
  • 1
  • 9
0

You are trying to execute php from js , that is impossible , you can do that in another way , like putting that result of echo do_shortcode('[flat9]'); in a hidden html tag and on click button , get it and concatenate it using js or put it in var using js like :

var code = "<?php echo do_shortcode('[flat9]'); ?>";
        document.getElementById("floorplan").innerHTML = "PHP"+code;
Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43