Title: Write shortcode inside javascript file
Last modified: October 21, 2020

---

# Write shortcode inside javascript file

 *  [oscarsk](https://wordpress.org/support/users/oscarsk/)
 * (@oscarsk)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/write-shortcode-inside-javascript-file/)
 * Hi
    How can I add a shortcode inside a javascript file?
 * I have the Astra Pro theme to my website, and have written this code:
 *     ```
       $(document).ready(function(){
   
       $( ".site-branding" ).append( "<?php echo do_shortcode('[astra_woo_mini_cart]'); ?>" );
   
       });
       ```
   
 * I have also tryed to add the code into the header.php and footer.php, but nothing
   happen?
 * Note: I have enabled [https://code.jquery.com/jquery-3.5.1.min.js](https://code.jquery.com/jquery-3.5.1.min.js)
   in my header.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [5 years, 7 months ago](https://wordpress.org/support/topic/write-shortcode-inside-javascript-file/#post-13563117)
 * You can’t. Javascript is executed in the browser. PHP is executed on the server.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/write-shortcode-inside-javascript-file/#post-13563327)
 * Steven is correct, but what you could do is assign the content returned from `
   do_shortcode()` (instead of echoing) to an array element passed to `wp_localize_script()`(
   all server side). This will assign the content to a JS object property on the
   page client side, which can be referenced by other JS code.
    [https://developer.wordpress.org/reference/functions/wp_localize_script/](https://developer.wordpress.org/reference/functions/wp_localize_script/)
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [5 years, 7 months ago](https://wordpress.org/support/topic/write-shortcode-inside-javascript-file/#post-13563330)
 * oohhh… yeah. I forgot about that!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Write shortcode inside javascript file’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 3 participants
 * Last reply from: [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * Last activity: [5 years, 7 months ago](https://wordpress.org/support/topic/write-shortcode-inside-javascript-file/#post-13563330)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
