Skip to content
Advertisement

How to add shortcode in wordpress using Javascript?

I want to put a shortcode in a WordPress website using JavaScript.

Website structure is

JavaScript

Firstly, i put div element to above wc-pr using below code.

JavaScript

Then i tried below code.

JavaScript

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.

Advertisement