1

I'm using a Thematic childtheme for my wordpress website, and instead of putting all my functions into the functions.php file, I thought I make it more organised by creating multiple other function files (admin_functions.php, single_functions.php ...) and included these files in my childtheme's functions.php file as follows:

require_once ('my-functions/admin-functions.php');
require_once ('my-functions/single-functions.php');

This works perfectly fine.

However, I'm trying to make wordpress load only the needed files by using the conditional tags as follows:

if(is_admin())
   require_once ('my-functions/admin-functions.php');
elseif(is_single())
   require_once ('my-functions/single-functions.php');

and this just won't work.

I can't seem to find a reason why. I thought that maybe there are some wordpress functions that must be called first before the conditional functions (is_single and is_admin) are usable. I can't seem to get anywhere though.

Any help is most appreciated. Thanks

  • I personally have never seen the layered approach to functions.php like that. Did you find this approach documented somewhere? – jdm2112 Aug 25 '14 at 15:50
  • This should work. Are you getting any errors? – Dan Bough Aug 25 '14 at 15:55
  • @jdm2112 I didn't find this anywhere. Im just trying to test if its do-able to check if the website would load faster. – kassemEzz Aug 25 '14 at 18:08
  • @DanBough Im getting the error: Fatal error: Call to undefined function – kassemEzz Aug 25 '14 at 18:09
1

As this is an important question with no real answer, I will provide what I found after hunting a solution down, though I don't have an answer as to why it doesn't work like we all think it should.

You need to hook into WordPress somewhere, like this:

// load our posts-only PHP
add_action( "wp", "only_posts" );
function only_posts() {
    if( is_single() ) {
        // we are on a single post
        include_once( "posts.php" );
    }
}

So in your case specifically, I would suggest writing it as follows to get is_single and is_admin to work in functions.php:

add_action( "wp", "include_conditionals" );
function include_conditionals() { 
    if( is_admin() ) {
        require_once( "my-functions/admin-functions.php" );
    } else if( is_single() ) {
        require_once( "my-functions/single-functions.php" );
    }
}

See here for more information.

2

First let's go ever when and where you can use these conditionals:

  • is_admin() would work pretty much universally. WP_ADMIN constant it checks for is defined extremely early in admin entry points and by exclusion if it's not there you aren't there
  • is_single() is much less robust, ir relies on global $wp_query being set up, such conditionals are primarily meant to be used inside theme's template files

Second, there is not much point for conditional loading. WordPress itself does this, but frankly it's questionable win (more so with how opcode caching works in reality, which any serious site should be using) and a lot of inconvenience when you do need functions in "wrong' context.

For classes this is primarily handled with autoload. For functions however the path of least resistance is just to load them and be done with it.

  • Thx for the answer. So you mean a function is only looked up by the php server when it gets called? Does the php server not do something like parsing the php file first then creating something like a register to where each function can be found? Also, doesn't a larger functions.php file take longer to load and hence slowing down the webpage loading? – kassemEzz Aug 25 '14 at 18:07
  • 1
    I don't mean that, I meant that load process depends on server configuration and that it's questionable if you are actually achieving anything by conditional load of definitions (especially on small scale). If you are interested in real performance improvement — profile the code, don't do random "optimizations". – Rarst Aug 25 '14 at 18:12
  • Ah OK I get what you mean and I agree with what you say that profiling the code will yield better results. Thanks for the very helpful info, but from a programming point of view I'm now just interested in my original question of why my code is not working :-) – kassemEzz Aug 25 '14 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.