• 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 4 months, 3 weeks ago by wpshushu.
    • This topic was modified 4 months, 3 weeks ago by wpshushu.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

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

    add_action( 'after_setup_theme', function () {
    foo( 'bar' );
    } );
    Thread Starter wpshushu

    (@wpshushu)

    @bungeshea Snippet can access MyClass::MYCONSTANT which is also defined in functions.php, though.

    Thread Starter wpshushu

    (@wpshushu)

    @bungeshea My mistake, snippets can access MyClass::MYCONSTANT is because it was called in a HOOK.

    Thread Starter wpshushu

    (@wpshushu)

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.