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
}