abromley
Forum Replies Created
Viewing 3 replies - 1 through 3 (of 3 total)
-
Forum: Fixing WordPress
In reply to: Functions within code snippets@threadi , thank you.
I’ve been experimenting a bit but cannot get the snippet to run properly if my nested function is coded outside the main function. I’m wondering if this is how it works:
- As the page is loaded, it finds the ‘shortcode’ reference. The reference includes the name of the main function
- It calls that function; and can call only that function. Any other functions written outside the main function do not work, even if called from within the main function
- So, the only way to get it to work, untidy as it is, is to nest.
Perhaps ‘code snippets’ are intended to run just 1 function – the named function. Perhaps I need to write it as a plugin.
Forum: Fixing WordPress
In reply to: Functions within code snippetsThe original code snippet is ‘show_time’. I have added (nested) another function that converts windspeed. It works – it displays both the time and the converted widspeed. But is what I’ve done ‘correct’?
function show_current_time() {
// Set your timezone (optional, default is UTC)
date_default_timezone_set('Europe/London');
// Format the current time
$current_time = date('l, d F Y H:i:s'); // Example: Monday, 04 June 2025 14:37:00
// Return the formatted time
$output = "Current Server Time: $current_time";
//
// my nested function
function convert_windspeed($speed) {
return $speed * 1.944;
}
// Here, an api call that gets windspeed. This works. Lets say the windpeed is 6 m/s.
$speed_ms = 6;
$output .= "Windspeed:" . convert_windspeed($speed_ms) . " kts";
return $output;
}
// Register shortcode [show_time]
add_shortcode('show_time', 'show_current_time');- This reply was modified 8 months, 2 weeks ago by abromley.
Forum: Fixing WordPress
In reply to: Functions within code snippetsHi,
.. the site is only a test of a test, and doesn’t have a cert at the mo.
I’ll try and replicate, in a few lines, what I mean and get back.
Thanks
Viewing 3 replies - 1 through 3 (of 3 total)