• All,

    Simple question from a newbie.

    I have a code snippet, called by shortcode, that works. It contains 1 function.

    I want to embed within this code snippet another function (to convert speeds from m/s to knots).

    Can this be done? If so – where would I put my function – within the main function, outside at the top or bottom?

    Or how else might I do this?

    Advice appreciated.

    Adrian

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator threadi

    (@threadi)

    Unfortunately, I am unable to access your site at the moment. Therefore, I am uncertain about the codes in question.

    Theoretically, it is possible to execute a shortcode within another PHP code. Example:

    function a() {
    $value = do_shortcode('[myshortcode]');
    // so something with this value.
    }

    However, I am unable to determine whether this is what you are looki

    Thread Starter abromley

    (@abromley)

    Hi,

    .. 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

    Thread Starter abromley

    (@abromley)

    The 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.
    Moderator threadi

    (@threadi)

    Yes, you can do that. For the sake of clarity, however, I would write the function outside of the “show_current_time” function. This would make it easier to read and also compatible with future PHP versions.

    Thread Starter abromley

    (@abromley)

    @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.

    Moderator threadi

    (@threadi)

    No, this is purely about PHP programming. It doesn’t know anything about code snippets. When a shortcode is executed, only PHP code is executed. And that can be written in any way that PHP allows. I now use PHP objects in some of my plugins to provide dynamic shortcodes based on central configurations.

    If storing the function outside doesn’t work for you, it’s probably related to how you integrate the code into your project. Do you use the functions.php of your child theme or a code snippet plugin for this?

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

The topic ‘Functions within code snippets’ is closed to new replies.