I am wondering if the below logic is correct and if certain circumstances should be present for it to work, since in some cases it does not seem to be working.
The example below is quite simple. Let's say that I want to use a function, that is defined elsewhere in a theme-related file, let's say parent_theme_hooks.php
, through an action hook in my child-theme functions.php
.
parent_theme_hooks.php
function is_enabled(){
return true;
}
function check_if_enabled(){
do_action( 'my_hook', $some, $args );
}
Then in the child-theme functions.php
function my_function($some, $args) {
if ( is_enabled() ) {
$message = 'yes';
} else {
$message = 'no';
}
echo $message;
}
add_action( 'my_hook', 'my_function', 11, 2 );
Question
So my question is if I can use the function is_enabled()
in the child-theme functions.php
when it is defined elsewhere in the parent theme?
Thanks
I am wondering if the below logic is correct and if certain circumstances should be present for it to work, since in some cases it does not seem to be working.
The example below is quite simple. Let's say that I want to use a function, that is defined elsewhere in a theme-related file, let's say parent_theme_hooks.php
, through an action hook in my child-theme functions.php
.
parent_theme_hooks.php
function is_enabled(){
return true;
}
function check_if_enabled(){
do_action( 'my_hook', $some, $args );
}
Then in the child-theme functions.php
function my_function($some, $args) {
if ( is_enabled() ) {
$message = 'yes';
} else {
$message = 'no';
}
echo $message;
}
add_action( 'my_hook', 'my_function', 11, 2 );
Question
So my question is if I can use the function is_enabled()
in the child-theme functions.php
when it is defined elsewhere in the parent theme?
Thanks
Yes, you can. But you have to be careful, since some special cases may occur. Some of them are:
class SomeClass {
...
function some_function() { ... }
...
}
In such case, you can use it only if: - it's a static function - it's public method and you have access to some object of that class.
function do_something() {
if ( condition_met() ) {
include_once( 'parent_theme_hooks.php' );
}
}
In such case the functions from that file won't be accessible always.
if ( class_exists( 'SomeClass' ) ) {
function some_function() { ... }
}
Again - if given class doesn't exist, then the function won't be accessible.
If the function comes with a plugin or a theme, then it may be declared using some action hook.
In such case the function won't be accessible before that hook is fired up.
Another example of this case is using functions from parent theme inside child theme. functions.php
file from parent theme comes after the same file from child theme. So you can't use parent functions directly in child themes functions.php file.
There are some ways you can make your code more reliable when using such functions:
if ( function_exists( 'function_name' ) )
and call it only if it does. You can also provide some alternative solution (fallback), if it doesn't exist.You can use functions defined in the parent theme, but you can only use them after the parent theme is loaded. The parent theme is loaded after your child theme.
This means that if you want to use a function from the parent theme you can only do it inside a function that is hooked into a hook on after_setup_theme
or later.
So your example will only work if check_if_enabled()
is run after the parent theme has loaded. If you attempt to use that function in your child theme before the parent theme has loaded, it won't work.
if (function_exists('is_enabled') && is_enabled())
, but if the do_action and the function are in the same file like this you will not have that problem. – majick Commented Nov 28, 2018 at 12:23function_exists
that gives me an extra check. Actually thedo_action
and the functionis_enabled
are not in the same file. Just to be sure, when the functions are loaded, I should be able to call these functions in the child-themefunctions.php
correct? – RobbTe Commented Nov 28, 2018 at 12:26function_exists
. That will just stop the code working if the function hasn't loaded, and what's the point of that? You shouldn't be trying to use it before it has loaded. The important thing is to understand when it has loaded, and only use it when it is safe to. – Jacob Peattie Commented Nov 28, 2018 at 12:27