Title: Functions within code snippets
Last modified: July 28, 2025

---

# Functions within code snippets

 *  [abromley](https://wordpress.org/support/users/abromley/)
 * (@abromley)
 * [8 months, 2 weeks ago](https://wordpress.org/support/topic/functions-within-code-snippets/)
 * 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](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Ffunctions-within-code-snippets%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [8 months, 2 weeks ago](https://wordpress.org/support/topic/functions-within-code-snippets/#post-18573890)
 * 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:
 *     ```wp-block-code
       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](https://wordpress.org/support/users/abromley/)
 * (@abromley)
 * [8 months, 2 weeks ago](https://wordpress.org/support/topic/functions-within-code-snippets/#post-18574055)
 * 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](https://wordpress.org/support/users/abromley/)
 * (@abromley)
 * [8 months, 2 weeks ago](https://wordpress.org/support/topic/functions-within-code-snippets/#post-18574313)
 * 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’?
 *     ```wp-block-code
       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 functionfunction 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](https://wordpress.org/support/users/abromley/).
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [8 months, 2 weeks ago](https://wordpress.org/support/topic/functions-within-code-snippets/#post-18574987)
 * 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](https://wordpress.org/support/users/abromley/)
 * (@abromley)
 * [8 months, 2 weeks ago](https://wordpress.org/support/topic/functions-within-code-snippets/#post-18577400)
 * [@threadi](https://wordpress.org/support/users/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](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [8 months, 2 weeks ago](https://wordpress.org/support/topic/functions-within-code-snippets/#post-18577978)
 * 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.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [threadi](https://wordpress.org/support/users/threadi/)
 * Last activity: [8 months, 2 weeks ago](https://wordpress.org/support/topic/functions-within-code-snippets/#post-18577978)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
