简体   繁体   中英

Add shortcode using javascript WordPress

I am using WordPress and I have replace some content inside a div using Javascript. It is working.

<script type="text/javascript">
var yourHTML = '<div class="mydiv">Test</div>';
document.getElementsByClassName('services-right')[0].innerHTML = yourHTML;
</script>

Now I want to insert a shortcode [jw_easy_logo slider_name="partners"] but it is not working when I am inserting then nothing happen on front-end.

<script type="text/javascript">
var yourHTML = '<div class="mydiv">[jw_easy_logo slider_name="partners"]</div>';
document.getElementsByClassName('services-right')[0].innerHTML = yourHTML;
</script>

Shortcode is not working but normal HTML is working.

Try this code

First get the output from shortcode using the do_shortcode() .

<script type="text/javascript">
    var yourHTML = '<div class="mydiv"><?php do_shortcode("[jw_easy_logo slider_name=partners]");?></div>';
    document.getElementsByClassName('services-right')[0].innerHTML = yourHTML;
</script>

NOTE: this code is working if shortcode is used the return not echo

in addition to what @Ankur said: "NOTE: this code is working if shortcode is used the return not echo"

even if the shortcode echos, you can use ob_start() and ob_get_clean() around the shortcode, so it won't print it striaght away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM