0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.