I want to put a shortcode in a WordPress website using JavaScript.
Website structure is
<div class="shop-container"> <div class="wrap">[shortcode here]</div> <div class="wc-pr"> </div> </div>
Firstly, i put div element to above wc-pr using below code.
<script> const parent5 = document.querySelector('.shop-container'); const billingField1 = document.querySelector('.wc-pr'); const newDiv = document.createElement('div'); newDiv.setAttribute('id', 'wrap'); parent5.insertBefore(newDiv, billingField1); </script>
Then i tried below code.
newDiv.innerHTML = `<?php do_shortcode("[shortcode here]");?>`; newDiv.innerText = `<?php do_shortcode("[shortcode here]");?>`;
But not working. innertext is diplayed as raw, Hhtml is not showing.
Please let me know how do i solve this
Thank you.
Advertisement
Answer
You are creating/generating an HTML, so you must echo it to the script. So just adding echo to the PHP
newDiv.innerHTML = '<?php echo do_shortcode("[shortcode here]");?>';
instead of
newDiv.innerHTML = '<?php do_shortcode("[shortcode here]");?>';
should do it.