Proper way of using functions in action hook?











up vote
1
down vote

favorite












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










share|improve this question




















  • 1




    yes. if you have reason to believe the file with the function may not have loaded, you can use say 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
    Nov 28 at 12:23






  • 1




    As long as a function is loaded when your hook is fired, you can use it.
    – butlerblog
    Nov 28 at 12:24










  • Thanks for the function_exists that gives me an extra check. Actually the do_action and the function is_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-theme functions.php correct?
    – RobbTe
    Nov 28 at 12:26










  • You shouldn't need to use function_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
    Nov 28 at 12:27










  • @JacobPeattie Thanks. I understand that I should only use a function once it has loaded. That is why I am asking this. In the logic provided above, the is_enabled() function has been loaded correct?
    – RobbTe
    Nov 28 at 12:32















up vote
1
down vote

favorite












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










share|improve this question




















  • 1




    yes. if you have reason to believe the file with the function may not have loaded, you can use say 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
    Nov 28 at 12:23






  • 1




    As long as a function is loaded when your hook is fired, you can use it.
    – butlerblog
    Nov 28 at 12:24










  • Thanks for the function_exists that gives me an extra check. Actually the do_action and the function is_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-theme functions.php correct?
    – RobbTe
    Nov 28 at 12:26










  • You shouldn't need to use function_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
    Nov 28 at 12:27










  • @JacobPeattie Thanks. I understand that I should only use a function once it has loaded. That is why I am asking this. In the logic provided above, the is_enabled() function has been loaded correct?
    – RobbTe
    Nov 28 at 12:32













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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










share|improve this question















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







functions hooks child-theme parent-theme






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 at 12:19









Krzysiek Dróżdż

13.2k52741




13.2k52741










asked Nov 28 at 12:17









RobbTe

467




467








  • 1




    yes. if you have reason to believe the file with the function may not have loaded, you can use say 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
    Nov 28 at 12:23






  • 1




    As long as a function is loaded when your hook is fired, you can use it.
    – butlerblog
    Nov 28 at 12:24










  • Thanks for the function_exists that gives me an extra check. Actually the do_action and the function is_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-theme functions.php correct?
    – RobbTe
    Nov 28 at 12:26










  • You shouldn't need to use function_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
    Nov 28 at 12:27










  • @JacobPeattie Thanks. I understand that I should only use a function once it has loaded. That is why I am asking this. In the logic provided above, the is_enabled() function has been loaded correct?
    – RobbTe
    Nov 28 at 12:32














  • 1




    yes. if you have reason to believe the file with the function may not have loaded, you can use say 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
    Nov 28 at 12:23






  • 1




    As long as a function is loaded when your hook is fired, you can use it.
    – butlerblog
    Nov 28 at 12:24










  • Thanks for the function_exists that gives me an extra check. Actually the do_action and the function is_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-theme functions.php correct?
    – RobbTe
    Nov 28 at 12:26










  • You shouldn't need to use function_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
    Nov 28 at 12:27










  • @JacobPeattie Thanks. I understand that I should only use a function once it has loaded. That is why I am asking this. In the logic provided above, the is_enabled() function has been loaded correct?
    – RobbTe
    Nov 28 at 12:32








1




1




yes. if you have reason to believe the file with the function may not have loaded, you can use say 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
Nov 28 at 12:23




yes. if you have reason to believe the file with the function may not have loaded, you can use say 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
Nov 28 at 12:23




1




1




As long as a function is loaded when your hook is fired, you can use it.
– butlerblog
Nov 28 at 12:24




As long as a function is loaded when your hook is fired, you can use it.
– butlerblog
Nov 28 at 12:24












Thanks for the function_exists that gives me an extra check. Actually the do_action and the function is_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-theme functions.php correct?
– RobbTe
Nov 28 at 12:26




Thanks for the function_exists that gives me an extra check. Actually the do_action and the function is_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-theme functions.php correct?
– RobbTe
Nov 28 at 12:26












You shouldn't need to use function_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
Nov 28 at 12:27




You shouldn't need to use function_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
Nov 28 at 12:27












@JacobPeattie Thanks. I understand that I should only use a function once it has loaded. That is why I am asking this. In the logic provided above, the is_enabled() function has been loaded correct?
– RobbTe
Nov 28 at 12:32




@JacobPeattie Thanks. I understand that I should only use a function once it has loaded. That is why I am asking this. In the logic provided above, the is_enabled() function has been loaded correct?
– RobbTe
Nov 28 at 12:32










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Yes, you can. But you have to be careful, since some special cases may occur. Some of them are:



1. Function may be class method and not a global function:



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.



2. File with that function may not be included always:



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.



3. Function may be declared inside if statement:



if ( class_exists( 'SomeClass' ) ) {
function some_function() { ... }
}


Again - if given class doesn't exist, then the function won't be accessible.



4. File is not loaded yet



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.



But...



There are some ways you can make your code more reliable when using such functions:




  1. Always try to understand when and how is that function declared.

  2. Check if the function exists with 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.






share|improve this answer




























    up vote
    2
    down vote













    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.






    share|improve this answer





















    • Thanks, in the example above, the check_if_enabled() is a function in the parent theme so that should work. I was especially wondering if i can use the function is_enabled() through the my_hook action in the child-theme functions.php. Thanks
      – RobbTe
      Nov 28 at 12:30










    • Only if my_hook is executed after the parent theme is loaded, and since that hook runs inside check_if_enabled() it depends entirely on when check_if_enabled() is run.
      – Jacob Peattie
      Nov 28 at 12:35






    • 1




      As I have said, it depends on when you use check_if_enabled(). If you're not using it in your child theme, it's fine, if you are, then it needs to be used in a hook that runs after the parent theme has loaded.
      – Jacob Peattie
      Nov 28 at 12:41






    • 1




      It does matter if the file that contains the definition has been included or not. If I define a function in a parent theme's functions.php file, and try to use it in the child theme outside a hook, I will get a "Call to undefined" error. This is because the parent theme's functions.php file has not been included yet. This applies to plugins too. If you try to use a function from a plugin that loads after yours you will get an error unless you're running the function after the plugins_loaded hook has run.
      – Jacob Peattie
      Nov 28 at 13:38








    • 1




      That being said, you should not be running any code in a theme outside of a hook anyway. But OP asked what circumstances were necessary for parent functions to work, and this is one of them.
      – Jacob Peattie
      Nov 28 at 13:42











    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "110"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f320448%2fproper-way-of-using-functions-in-action-hook%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Yes, you can. But you have to be careful, since some special cases may occur. Some of them are:



    1. Function may be class method and not a global function:



    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.



    2. File with that function may not be included always:



    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.



    3. Function may be declared inside if statement:



    if ( class_exists( 'SomeClass' ) ) {
    function some_function() { ... }
    }


    Again - if given class doesn't exist, then the function won't be accessible.



    4. File is not loaded yet



    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.



    But...



    There are some ways you can make your code more reliable when using such functions:




    1. Always try to understand when and how is that function declared.

    2. Check if the function exists with 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.






    share|improve this answer

























      up vote
      2
      down vote



      accepted










      Yes, you can. But you have to be careful, since some special cases may occur. Some of them are:



      1. Function may be class method and not a global function:



      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.



      2. File with that function may not be included always:



      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.



      3. Function may be declared inside if statement:



      if ( class_exists( 'SomeClass' ) ) {
      function some_function() { ... }
      }


      Again - if given class doesn't exist, then the function won't be accessible.



      4. File is not loaded yet



      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.



      But...



      There are some ways you can make your code more reliable when using such functions:




      1. Always try to understand when and how is that function declared.

      2. Check if the function exists with 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.






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Yes, you can. But you have to be careful, since some special cases may occur. Some of them are:



        1. Function may be class method and not a global function:



        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.



        2. File with that function may not be included always:



        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.



        3. Function may be declared inside if statement:



        if ( class_exists( 'SomeClass' ) ) {
        function some_function() { ... }
        }


        Again - if given class doesn't exist, then the function won't be accessible.



        4. File is not loaded yet



        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.



        But...



        There are some ways you can make your code more reliable when using such functions:




        1. Always try to understand when and how is that function declared.

        2. Check if the function exists with 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.






        share|improve this answer












        Yes, you can. But you have to be careful, since some special cases may occur. Some of them are:



        1. Function may be class method and not a global function:



        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.



        2. File with that function may not be included always:



        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.



        3. Function may be declared inside if statement:



        if ( class_exists( 'SomeClass' ) ) {
        function some_function() { ... }
        }


        Again - if given class doesn't exist, then the function won't be accessible.



        4. File is not loaded yet



        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.



        But...



        There are some ways you can make your code more reliable when using such functions:




        1. Always try to understand when and how is that function declared.

        2. Check if the function exists with 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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 at 12:30









        Krzysiek Dróżdż

        13.2k52741




        13.2k52741
























            up vote
            2
            down vote













            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.






            share|improve this answer





















            • Thanks, in the example above, the check_if_enabled() is a function in the parent theme so that should work. I was especially wondering if i can use the function is_enabled() through the my_hook action in the child-theme functions.php. Thanks
              – RobbTe
              Nov 28 at 12:30










            • Only if my_hook is executed after the parent theme is loaded, and since that hook runs inside check_if_enabled() it depends entirely on when check_if_enabled() is run.
              – Jacob Peattie
              Nov 28 at 12:35






            • 1




              As I have said, it depends on when you use check_if_enabled(). If you're not using it in your child theme, it's fine, if you are, then it needs to be used in a hook that runs after the parent theme has loaded.
              – Jacob Peattie
              Nov 28 at 12:41






            • 1




              It does matter if the file that contains the definition has been included or not. If I define a function in a parent theme's functions.php file, and try to use it in the child theme outside a hook, I will get a "Call to undefined" error. This is because the parent theme's functions.php file has not been included yet. This applies to plugins too. If you try to use a function from a plugin that loads after yours you will get an error unless you're running the function after the plugins_loaded hook has run.
              – Jacob Peattie
              Nov 28 at 13:38








            • 1




              That being said, you should not be running any code in a theme outside of a hook anyway. But OP asked what circumstances were necessary for parent functions to work, and this is one of them.
              – Jacob Peattie
              Nov 28 at 13:42















            up vote
            2
            down vote













            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.






            share|improve this answer





















            • Thanks, in the example above, the check_if_enabled() is a function in the parent theme so that should work. I was especially wondering if i can use the function is_enabled() through the my_hook action in the child-theme functions.php. Thanks
              – RobbTe
              Nov 28 at 12:30










            • Only if my_hook is executed after the parent theme is loaded, and since that hook runs inside check_if_enabled() it depends entirely on when check_if_enabled() is run.
              – Jacob Peattie
              Nov 28 at 12:35






            • 1




              As I have said, it depends on when you use check_if_enabled(). If you're not using it in your child theme, it's fine, if you are, then it needs to be used in a hook that runs after the parent theme has loaded.
              – Jacob Peattie
              Nov 28 at 12:41






            • 1




              It does matter if the file that contains the definition has been included or not. If I define a function in a parent theme's functions.php file, and try to use it in the child theme outside a hook, I will get a "Call to undefined" error. This is because the parent theme's functions.php file has not been included yet. This applies to plugins too. If you try to use a function from a plugin that loads after yours you will get an error unless you're running the function after the plugins_loaded hook has run.
              – Jacob Peattie
              Nov 28 at 13:38








            • 1




              That being said, you should not be running any code in a theme outside of a hook anyway. But OP asked what circumstances were necessary for parent functions to work, and this is one of them.
              – Jacob Peattie
              Nov 28 at 13:42













            up vote
            2
            down vote










            up vote
            2
            down vote









            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 28 at 12:26









            Jacob Peattie

            15k41826




            15k41826












            • Thanks, in the example above, the check_if_enabled() is a function in the parent theme so that should work. I was especially wondering if i can use the function is_enabled() through the my_hook action in the child-theme functions.php. Thanks
              – RobbTe
              Nov 28 at 12:30










            • Only if my_hook is executed after the parent theme is loaded, and since that hook runs inside check_if_enabled() it depends entirely on when check_if_enabled() is run.
              – Jacob Peattie
              Nov 28 at 12:35






            • 1




              As I have said, it depends on when you use check_if_enabled(). If you're not using it in your child theme, it's fine, if you are, then it needs to be used in a hook that runs after the parent theme has loaded.
              – Jacob Peattie
              Nov 28 at 12:41






            • 1




              It does matter if the file that contains the definition has been included or not. If I define a function in a parent theme's functions.php file, and try to use it in the child theme outside a hook, I will get a "Call to undefined" error. This is because the parent theme's functions.php file has not been included yet. This applies to plugins too. If you try to use a function from a plugin that loads after yours you will get an error unless you're running the function after the plugins_loaded hook has run.
              – Jacob Peattie
              Nov 28 at 13:38








            • 1




              That being said, you should not be running any code in a theme outside of a hook anyway. But OP asked what circumstances were necessary for parent functions to work, and this is one of them.
              – Jacob Peattie
              Nov 28 at 13:42


















            • Thanks, in the example above, the check_if_enabled() is a function in the parent theme so that should work. I was especially wondering if i can use the function is_enabled() through the my_hook action in the child-theme functions.php. Thanks
              – RobbTe
              Nov 28 at 12:30










            • Only if my_hook is executed after the parent theme is loaded, and since that hook runs inside check_if_enabled() it depends entirely on when check_if_enabled() is run.
              – Jacob Peattie
              Nov 28 at 12:35






            • 1




              As I have said, it depends on when you use check_if_enabled(). If you're not using it in your child theme, it's fine, if you are, then it needs to be used in a hook that runs after the parent theme has loaded.
              – Jacob Peattie
              Nov 28 at 12:41






            • 1




              It does matter if the file that contains the definition has been included or not. If I define a function in a parent theme's functions.php file, and try to use it in the child theme outside a hook, I will get a "Call to undefined" error. This is because the parent theme's functions.php file has not been included yet. This applies to plugins too. If you try to use a function from a plugin that loads after yours you will get an error unless you're running the function after the plugins_loaded hook has run.
              – Jacob Peattie
              Nov 28 at 13:38








            • 1




              That being said, you should not be running any code in a theme outside of a hook anyway. But OP asked what circumstances were necessary for parent functions to work, and this is one of them.
              – Jacob Peattie
              Nov 28 at 13:42
















            Thanks, in the example above, the check_if_enabled() is a function in the parent theme so that should work. I was especially wondering if i can use the function is_enabled() through the my_hook action in the child-theme functions.php. Thanks
            – RobbTe
            Nov 28 at 12:30




            Thanks, in the example above, the check_if_enabled() is a function in the parent theme so that should work. I was especially wondering if i can use the function is_enabled() through the my_hook action in the child-theme functions.php. Thanks
            – RobbTe
            Nov 28 at 12:30












            Only if my_hook is executed after the parent theme is loaded, and since that hook runs inside check_if_enabled() it depends entirely on when check_if_enabled() is run.
            – Jacob Peattie
            Nov 28 at 12:35




            Only if my_hook is executed after the parent theme is loaded, and since that hook runs inside check_if_enabled() it depends entirely on when check_if_enabled() is run.
            – Jacob Peattie
            Nov 28 at 12:35




            1




            1




            As I have said, it depends on when you use check_if_enabled(). If you're not using it in your child theme, it's fine, if you are, then it needs to be used in a hook that runs after the parent theme has loaded.
            – Jacob Peattie
            Nov 28 at 12:41




            As I have said, it depends on when you use check_if_enabled(). If you're not using it in your child theme, it's fine, if you are, then it needs to be used in a hook that runs after the parent theme has loaded.
            – Jacob Peattie
            Nov 28 at 12:41




            1




            1




            It does matter if the file that contains the definition has been included or not. If I define a function in a parent theme's functions.php file, and try to use it in the child theme outside a hook, I will get a "Call to undefined" error. This is because the parent theme's functions.php file has not been included yet. This applies to plugins too. If you try to use a function from a plugin that loads after yours you will get an error unless you're running the function after the plugins_loaded hook has run.
            – Jacob Peattie
            Nov 28 at 13:38






            It does matter if the file that contains the definition has been included or not. If I define a function in a parent theme's functions.php file, and try to use it in the child theme outside a hook, I will get a "Call to undefined" error. This is because the parent theme's functions.php file has not been included yet. This applies to plugins too. If you try to use a function from a plugin that loads after yours you will get an error unless you're running the function after the plugins_loaded hook has run.
            – Jacob Peattie
            Nov 28 at 13:38






            1




            1




            That being said, you should not be running any code in a theme outside of a hook anyway. But OP asked what circumstances were necessary for parent functions to work, and this is one of them.
            – Jacob Peattie
            Nov 28 at 13:42




            That being said, you should not be running any code in a theme outside of a hook anyway. But OP asked what circumstances were necessary for parent functions to work, and this is one of them.
            – Jacob Peattie
            Nov 28 at 13:42


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to WordPress Development Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f320448%2fproper-way-of-using-functions-in-action-hook%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?