Support » Fixing WordPress » functions.php –one function with conditional tags isn’t working

  • Hope someone can help me out.

    I had originally loaded a bunch of scripts for a site I’m working on in the header.php file, using conditional tags so that they only loaded on pages in which they were needed. In an attempt to use better practices, I’ve just moved everything over to the functions.php file. The problem is that all of the Conditional Tags don’t seem to be working correctly (if(is_home)){};, if(is_page()){};, etc.). They worked perfectly while in header.php.

    If I comment out all of the conditional tags, everything works perfectly. I also have several other functions in the file that use conditional tags, and those work well.

    I’ve also tried adding global $post; to the function, and that didn’t make a difference. (I can’t say I fully grasp when it is required yet).

    Here’s what I have:

    function my_scripts_init() {
    	$scriptinitDir = get_bloginfo('template_directory').'/js/';
    	if(!is_admin()) {
    		wp_register_script('my_sidebar_script', $scriptinitDir.'sidebar-nav.js', false);
    		wp_register_script('jquery', '', false, '1.3.2');
    		wp_enqueue_script('toggle_val', $scriptinitDir.'jquery.toggleval.js', array('jquery')); //default search text in header search bar
    		// swfObject for flash header on home page
    		if (is_home()) {
    			wp_enqueue_script('my_swf_object', $scriptinitDir.'swfobject.js', false);
    		// Different inits for Accordion Scripts
    		if (is_page('54') || $post->post_parent == '54') { //portfolio sidebar init
    			wp_enqueue_script('sidebar_nav_portfolio', $scriptinitDir.'portfolio.js', array('my_sidebar_script'));
    		elseif (is_page('2') || $post->post_parent == '2') { // rest of About sidebar init
    			wp_enqueue_script('sidebar_nav_about', $scriptinitDir.'about.js', array('my_sidebar_script'));
    		else { //All other sidebar init
    			wp_enqueue_script('sidebar_nav_init', $scriptinitDir.'sidebar-nav-init1.js', array('my_sidebar_script'));
    		// Zebra lists fpr the News & Press page
    		if ( is_page('45') || $post->post_parent == '2') {
    			wp_enqueue_script('my_zebra_stripes', $scriptinitDir.'jquery.alternate.js', array('jquery'));
    add_action('init', 'my_scripts_init');

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Kerri,

    I have a couple of observations about your code. However, a little disclaimer first: I am not a big php expert, so before trying anything back up your stuff, 🙂

    My first observation has to do with comments. You mentioned that if you remove your comments, everything works well. Sometimes, for some very strange reason, comments don’t get processed as they should by browsers (this does only happen with php, it also happens with css and html), so my first advice is, unless your client is going to be poking around the functions file, to just delete them;
    My second observation is related to code consistency in your if statements. I noticed that you add a “!” before the parameter “is_admin”, but you don’t before “is_home”, “is_page”, etc. Since both are correct, I would suggest that, just in case, you changed all to “!<parameter>” or just plain “<parameter>” (you never know, right?).

    The rest looks fine to me, but then again, not an expert.

    Hope that helped, at least a little bit! 🙂


    I had the same question, and it took me half of the day to figure it out. Now, I’m not going to be able to explain all of this, but I can share some code and credit the source that I used to figure this out.

    Basically, you need to use:
    add_action('wp_print_scripts', 'my_scripts_init');

    Instead of:
    add_action('init', 'my_scripts_init');

    Oh, I’ve just come back to this thread after a long hiatus! Thanks so much, 812studio, I haven’t tried that yet, but it looks very promising! Sometimes it just take the perfect googling luck to find what you need.

    Marventus, just to clarify a couple of things:
    I may not have been clear that what I meant is when I commented out the offending code (akin to removing it), then the rest worked. Not that my actual comments were breaking it.
    Also, maybe I misunderstood what you’re trying to say but if(is_foo) and if(!is_foo) have exactly opposite meanings, so I can’t just use either or. (if(is_foo) means “if the page is foo”. if(!is_foo) means “if the page is NOT foo” — very crucial difference!). What that code is saying is, “As long as this isn’t an admin page, do all the other stuff).

    The is_functions aren’t available at init if i’m not mistaken…

    You might instead need to be looking at the wp_query object, ie. if( $wp_query->is_home )..

    Also note, i don’t believe you need to check !is_admin at init, init doesn’t run in the admin, it has it’s own admin_init.

    However, like 812studio said above, you should be able to use the conditions if you instead use wp_print_scripts in place of init. You could even use wp_head as a last resort.

    Thanks, t31os_; that was very helpful info!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘functions.php –one function with conditional tags isn’t working’ is closed to new replies.