I am customising a WordPress admin area and so far so good with a number of add_filter and add_action calls in my theme’s functions.php file.

But I’m a bit worried because all this code will be executed on all pages, not just the admin area.

I therefore went ahead with the below approach:

if (is_admin())
    require_once('admin-functions.php');

It works but is this the cleanest way of doing it?

1 Answer
1

The way you do it is fine.

Also have a look at how Tom McFarlins WordPress Plugin Boilerplate does it:

if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {    
     require_once( plugin_dir_path( __FILE__ ) . 'admin/class-plugin-name-admin.php' );
     add_action( 'plugins_loaded', array( 'Plugin_Name_Admin', 'get_instance' ) );    
}

Source

Leave a Reply

Your email address will not be published. Required fields are marked *