Title: function defined in functions.php not callable from snippet code
Last modified: June 28, 2024

---

# function defined in functions.php not callable from snippet code

 *  Resolved [wpshushu](https://wordpress.org/support/users/wpshushu/)
 * (@wpshushu)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/function-defined-in-functions-php-not-callable-from-snippet-code/)
 * Hi,
 * I defined a function foo inside wp-content/themes/mytheme/functions.php, say 
   it looks like this:
 * function foo($msg = ”) { … error_log( $msg ); …. }
 * Then I created a snippet to call it, ended up with a undefined function error.
 * In the mean time, I have a custom class defined in functions.php too:
 * MyClass { public static MYCONSTANT = ….. }
 * The snippent can access MyClass::MYCONSTANT with no problem.
 * Why?
 * Thanks
    -  This topic was modified 1 year, 11 months ago by [wpshushu](https://wordpress.org/support/users/wpshushu/).
    -  This topic was modified 1 year, 11 months ago by [wpshushu](https://wordpress.org/support/users/wpshushu/).

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

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/function-defined-in-functions-php-not-callable-from-snippet-code/#post-17855590)
 * Hi [@wpshushu](https://wordpress.org/support/users/wpshushu/),
 * I suspect that this might be due to load order – snippets are loaded *before*
   theme files, so functions defined in theme files won’t be available immediately
   when snippets are loaded.
 * Have you tried calling the function inside a later action hook, e.g.:
 *     ```wp-block-code
       add_action( 'after_setup_theme', function () {    foo( 'bar' );} );
       ```
   
 *  Thread Starter [wpshushu](https://wordpress.org/support/users/wpshushu/)
 * (@wpshushu)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/function-defined-in-functions-php-not-callable-from-snippet-code/#post-17855608)
 * [@bungeshea](https://wordpress.org/support/users/bungeshea/) Snippet can access
   MyClass::MYCONSTANT which is also defined in functions.php, though.
 *  Thread Starter [wpshushu](https://wordpress.org/support/users/wpshushu/)
 * (@wpshushu)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/function-defined-in-functions-php-not-callable-from-snippet-code/#post-17855616)
 * [@bungeshea](https://wordpress.org/support/users/bungeshea/) My mistake, snippets
   can access MyClass::MYCONSTANT is because it was called in a HOOK.
 *  Thread Starter [wpshushu](https://wordpress.org/support/users/wpshushu/)
 * (@wpshushu)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/function-defined-in-functions-php-not-callable-from-snippet-code/#post-17855622)
 * [@bungeshea](https://wordpress.org/support/users/bungeshea/) What’s the best 
   place to put the definition of a custom function so it’s immediately callable
   by any code snippet, I mean not in any HOOK but as top level statements in the
   snippet?
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/function-defined-in-functions-php-not-callable-from-snippet-code/#post-18182131)
 * You should be able to add it as a snippet and set it to have a low number for
   its priority value.
 * Otherwise, including it in a plugin or must-use plugin should mean it’s available
   for snippets to use when they are loaded.

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

The topic ‘function defined in functions.php not callable from snippet code’ 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/)

 * 5 replies
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [1 year, 6 months ago](https://wordpress.org/support/topic/function-defined-in-functions-php-not-callable-from-snippet-code/#post-18182131)
 * Status: resolved