0

I have a bunch of functionality being added to a Wordpress site including files in functions.php but I'm worried about performance. All of these files are included for every page request and not all pages require all the extra functionality.

Drupal's menu system allows you to only include files for certain URLs and this is supposed to reduce overhead. Is there a similar approach for Wordpress or I shouldn't fret over this?

Thanks!

Update to clarify: I meant including PHP files with extra functionality in new classes or functions. I'm not talking about CSS, Javascript or templates, which of course, have their own ways of being included.

4
  • What exactly are you trying to include? You can use wp_enqueue_script and wp_enqueue_style to conditionally add in scripts and styles (View Codex) you can even conditionally include templates but you'll need to be more specific for what it is you're looking for. Commented May 5, 2014 at 18:36
  • Also, consider spl_autoload_register(), that loads files with undeclared classes. However this is a PHP functionality not directly related to Wordpress. Commented May 5, 2014 at 18:51
  • Thanks Howdy_McGee for noting that, I've updated my question to clarify. Commented May 6, 2014 at 19:22
  • I think between me and tf we have explained as best to our understanding to your question. Extra functions/php files can be conditionaly loaded according to page. This goes for classes as well. Commented May 7, 2014 at 6:32

2 Answers 2

1

Use WordPress' conditional tags, global variables, defines, class checks and the like. For instance like so:

<?php
// load helper functions - always
require_once get_stylesheet_directory() . '/inc/helper-functions.php';

// load admin functions - for back-end only
if ( is_admin() )
    require_once get_stylesheet_directory() . '/inc/admin-functions.php';

// load WooCommerce functions - when WooCommerce is active
if ( in_array(
    'woocommerce/woocommerce.php',
    apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) )
)
    require_once get_stylesheet_directory() . '/inc/woocommerce-functions.php';

// load SomeClass' functions - when class SomeClass is known
if ( class_exists( 'SomeClass' ) )
    require_once get_stylesheet_directory() . '/inc/someclass-functions.php';

// load debug functions - when debugging
if ( defined( 'WP_DEBUG' ) && WP_DEBUG )
    require_once get_stylesheet_directory() . '/inc/debug-functions.php';

You can also bail out when meeting certain criteria inside some function:

add_action( 'wp_enqueue_scripts', 'wpdev_143439_enqueue_scripts' );

function wpdev_143439_enqueue_scripts() {

    if (
        ! is_single()
        || is_singular( 'my_cpt' )
        || (
            defined( 'NEVERSCRIPT' )
            && NEVERSCRIPT
        )
    ) {
        return;
    }

    // Enqueue some scripts here
}
0
1

It is always better and more advisable to load functions/styles/scripts etc only on the pages where they are needed. I have never personally worked with Drupal, so I won't comment on their inner workings, but for Wordpress there are conditional tags that just do as the name states, checks if a certain condition is met, if true, the required 'action' is performed. If the condition is not met, ie return false, the 'action' is not performed.

Unnecessary loading of functions/styles/scripts etc do have an impact on performance, so you should plan what is loaded when, and anything that aren't necessary on all pages must then be loaded conditionally.

It is impossible to cover every aspect regarding the uses of conditional tags, so I would advise you to go and do some research, create yourself a local test site and go play around with conditionals. Here are some good reads on this subject

  1. Codex - Conditional tags

  2. PHP for WordPress: Mastering Conditional Statements and Tags by http://code.tutsplus.com/

  3. Google search conditional tags wordpress

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.