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
Thanks @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!”.
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/
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.
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/
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.
Ah nice, that’s exactly what I need. Thank you so much!