Title: Javascript snippet not executing
Last modified: February 28, 2022

---

# Javascript snippet not executing

 *  Resolved [pastafarian24](https://wordpress.org/support/users/pastafarian24/)
 * (@pastafarian24)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/javascript-snippet-not-executing/)
 * Hi,
 * I’ve successfully used the snippet plugin to add text snippets to my site, but
   when I try to add a javascript snippet, it just shows the shortcode [snippetname]
   on my site.
 * I created a new snippet with the name “reward” and the following content:
 *     ```
       add_action( 'wp_head', function () { ?>
       <script>
   
       	const euro = 3.03;
       	document.write(euro);
   
       </script>
       <?php } );
       ```
   
 * and tried including it on the page by simply writing `[reward]`. It’s probably
   a newbie mistake, but I couldn’t find a solution on this forum.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fjavascript-snippet-not-executing%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  [Elías](https://wordpress.org/support/users/eliasgdj/)
 * (@eliasgdj)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/javascript-snippet-not-executing/#post-15412833)
 * Where want you to use that euro variable?
 * Anyway, I think you have to do open and close and not only `write`: [https://developer.mozilla.org/en-US/docs/Web/API/Document/write](https://developer.mozilla.org/en-US/docs/Web/API/Document/write)
 *  Thread Starter [pastafarian24](https://wordpress.org/support/users/pastafarian24/)
 * (@pastafarian24)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/javascript-snippet-not-executing/#post-15414725)
 * Thanks [@eliasgdj](https://wordpress.org/support/users/eliasgdj/) for the tip.
   The code shouldn’t do much yet, as I was just trying to see if it would work 
   in general. When I paste the part inside the script tags into the w3schools tryit
   editor, it just shows the value of the euro variable. Imagine it as “Hello World!”.
 *  Thread Starter [pastafarian24](https://wordpress.org/support/users/pastafarian24/)
 * (@pastafarian24)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/javascript-snippet-not-executing/#post-15414824)
 * Okay, so I just noticed that every page now shows 3.03 on the top of the page.
   Didn’t see that before because of my caching plugin. That’s not at all what I
   was trying to do though. I thought it would just insert the snippet wherever 
   I called it, not on every page.
 * My goal was to be able to have numbers dynamically change. For example, I have
   a site where I show the current mining profits per kWh for different crypto miners:
   [https://mining-rigs-kaufen.de/welcher-crypto-miner-fuer-ihre-solaranlage/](https://mining-rigs-kaufen.de/welcher-crypto-miner-fuer-ihre-solaranlage/)
 * That changes daily, so I don’t want to always adjust every number on the page,
   just the variable “profit per 100 MH/s” which is then automatically divided by
   the power consumption of the mining rigs (always the same).
 * Do you know how I can use basic mathematical functions on my wordpress sites 
   to show dynamically changing content? I thought javascript was the way to go 
   here, but I’ve never used it before.
 *  [Elías](https://wordpress.org/support/users/eliasgdj/)
 * (@eliasgdj)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/javascript-snippet-not-executing/#post-15415141)
 * > I thought it would just insert the snippet wherever I called it, not on every
   > page.
 * You have added your function in the `wp_head`, so it will be called in the `head`
   element of the page. The snippets do not have shortcodes, unless you create one
   for the function, like this:
 *     ```
       function my_script() {
       //the function code
       }
       add_shortcode('euro_value', 'my_script');
       ```
   
 * You have more information here: [https://developer.wordpress.org/reference/functions/add_shortcode/](https://developer.wordpress.org/reference/functions/add_shortcode/)
 * >  Do you know how I can use basic mathematical functions on my wordpress sites
   > to show dynamically changing content?
 * You can do it with a shortcode, but the way to output it depends on how do you
   create your pages. It could be just like this:
 *     ```
       function my_euro_value() {
       echo "3.08";
       }
       add_shortcode('euro_value', 'my_euro_value');
       ```
   
 * And when you write `[euro_value]` on your content, the value will be shown instead.
 *  Thread Starter [pastafarian24](https://wordpress.org/support/users/pastafarian24/)
 * (@pastafarian24)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/javascript-snippet-not-executing/#post-15415359)
 * Ah nice, that’s exactly what I need. Thank you so much!

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

The topic ‘Javascript snippet not executing’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

## Tags

 * [execute](https://wordpress.org/support/topic-tag/execute/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [snippet](https://wordpress.org/support/topic-tag/snippet/)

 * 5 replies
 * 2 participants
 * Last reply from: [pastafarian24](https://wordpress.org/support/users/pastafarian24/)
 * Last activity: [4 years, 1 month ago](https://wordpress.org/support/topic/javascript-snippet-not-executing/#post-15415359)
 * Status: resolved