• You can’t use the conditional checks in the functions.php file, because the functions.php file loads before the page loads, so the conditional functions have nothing to check against.

    Like

    function load_js() {
        if(is_home()){
            wp_register_script('main.min.js', THEME_URL . 'js/main.min.js', false, '1.0', true);
            wp_enqueue_script('main.min.js');
        }
    }
    add_action('init', 'load_js');

    Just won’t work. Correct?

    I don’t remember where I stumbled across this, but I discovered a work around.

    if you add

    <?php do_action('init'); ?>

    after wp_head() the conditional checks in the functions.php work.

    I don’t now why this works, but it does.

    Does anyone have any idea why this does work>

Viewing 8 replies - 1 through 8 (of 8 total)
  • Why that worksis quite simple. You’re not running the code when the functions.php file loads. The call do add_action() sets that function to be run at a time in the future when the ‘init’ action is called. As it works using that action, that means that the ‘init’ action is called after everything that you need has been set up and is ready to work.

    BUT… You should NOT use the do_action('imit'); yourself. If you do, you’ll run the risk of breaking things that need some other section loaded that hasn’t loaded yet. On to of that it’s also called in the WordPress core code, so you’re calling every function attached to that action twice. Not only is that doubleing the load on your server, you’re also running the risk of things happening twice, and that can be anywhere from annoyiing to disasterous.

    Oh, and in anser to your actual question…

    It doesn’t work with

    add_action('init', 'load_js');

    because that’s thw wrong action. Adding this using the ‘init’ action its to early. On the codex page it spells it out even if it is a bit hidden – http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    You need to use this:

    add_action('wp_enqueue_scripts', 'load_js');

    Thread Starter gilgimech

    (@gilgimech)

    No,

    Sorry, maybe I didn’t explain it well.

    I’m not calling any function with the do_action().

    It’s literally just

    <?php do_action(‘init’); ?>

    with no function attached to it.

    for some reason just having that little snippet causes the conditionals to work in the other functions in the functions.php file. Where as if that snippet wasn’t in the head the conditionals don’t work.

    The load_js() function was just a demonstration. The function is irrelevant. Its the is_home() that’s the focus.

    OK, then you are doing it wrong.

    You should not be calling do_action() on any action that you didn’t create yourself in your own code.

    The conditional functions do rely on that, as you’ve seen, but you are trying ot force them before they’re ready, and ther’s got to be a better way of doing it.

    Exactly what are you trying to do that you need the is_home() function in your functions.php file? If you can give osme more info of what you actually want to do I’ll be able to help out and give you an idea of how to do it the right way.

    Thread Starter gilgimech

    (@gilgimech)

    OK,

    Never mind. You just don’t understand what I’m asking. This was just a fluke that I came across and I want to understand why it works. Is it just a bug, or is it something else?

    I’m not asking if it’s right or wrong. I don’t need help with any code. I just want to figure out why this works the way it does.

    It seems like a bug to me, and if I can figure it out or if someone might be able to help me figure it out. Then it can be reported and fixed.

    Just read my original post. The first this I said is that I understand why the conditionals don’t work in the functions.php file, but I stumbled onto this and I want to figure out why it works.

    I know exactly what you’re asking, but you don’t seem to understand what you’re doing wrong.

    The reason it works when you call do_actio('init') is that the functions that are attached to that function do the initialisation of the system, and those functions are what set up the values that you need to do what you’r trying to do.

    There’s no bug. It’s the design of the system so that it can use those functions when they are needed. This is how it’s supposed to work so that the system functions correctly. Conditional functions such as the one that you’ve given as an example should not be used in the functions.php file. They shoudl be used in functions that are attached to actions that are called at the appropriate time. That’s the entire point of the add_action() functions. They let you do things when they’re suppsoed to happen. Trying to shoe-hrom your fucntion in before that runs a very big risk of breaking a lot of other parts of the site, which doesn’t seme ot be a concern for you.

    Thread Starter gilgimech

    (@gilgimech)

    First off,

    Why are you assuming that I’m actually using this in a real site? I never stated or implied that I was. I just asked a question. This is just something I came acros a few months ago and digging through the core by myself hasn’t giving me any answers, so against my better judgment I decided to post it here hopping there might be someone that is willing to help me figure this out.

    If, you know exactly what I’m asking then why don’t you just answer my question instead of making assumptions.

    Yes, I know how conditional functions, add_action, and do_action() work.

    There are no functions attached to the do_action(‘init’). It’s just do_action(‘init’) all by itself with no other functions. do_action(‘init’) shouldn’t do anything (as far as I know). If anything, I think it should throw an error, but it doesn’t. What it does do is it allows the conditional function in the functions.php file even though they shouldn’t.

    So what’s causing that? are there some core function tied to init() causing this. If so what are they? Why would these functions be causing the conditional functions to work? That’s what I’m looking for.

    So, catacaustic. Please just stop posting unless you’re willing to help me figure this out.

    Ok, ok… :/ Most people that ask questions like that are asking for real sites, so that’s an ssumption that I made. Sorry that it was wrong.

    Now on to why… Just because you aren’t setting up anything with add_action ('init', 'function_name') doesn’t mean that nothing else is. There’s a whole heap of functions that use the init action. Some are from other plugins but most are in the core code in a wide range of places. Those functions are what sets up (initialises) the system. When you use do-action('init'); you’re calling each of those functions. So it’s one of those functions that sets up the page/post/etc, which is how the conditional function can get the values that they need.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Using conditional checks in the functions.php file’ is closed to new replies.