admin管理员组

文章数量:1122832

I have a function declared in my child theme's functions.php file as seen below:

function test_fn(){
error_log("Test fn");
}

When I am calling this function in a plugin file, I am getting call to an undefined function error. How to make test_fn() accessible in plugin files?

I have a function declared in my child theme's functions.php file as seen below:

function test_fn(){
error_log("Test fn");
}

When I am calling this function in a plugin file, I am getting call to an undefined function error. How to make test_fn() accessible in plugin files?

Share Improve this question asked Aug 8, 2024 at 19:46 dc09dc09 1952 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

you can create a hook to make communication between different parts :

in your theme :

add_filter("MY_THEME/creating_something", function ($return, $args1, $args2) {
    
    error_log("calling creating_something in " . __FILE__);
    
    if ($args2 > 2) {
        $return = 100 * $args1 + $args2;
    }
    
    return $return;
    
}, 10, 3); // 3 is for the 3 arguments of the anonymous function

and everywhere else you can use it like that :

$result = apply_filters("MY_THEME/creating_something", 45, 0, 0);
// return 45

$result = apply_filters("MY_THEME/creating_something", 45, 30, 9);
// return 3009

in my example, 10 is the common priority for hooks. you can use different priority if, e.g., you want to override this filter in you development environment, you can do that :

add_filter("MY_THEME/creating_something", function ($return, $args1, $args2) {
    
    return "It's dev here";
    
}, 500, 3);

本文标签: How to access a function declared in child theme39s functions file in a plugin file