When is is_admin() available?

When is is_admin() available?

Pretty much everywhere. The only example I can think of where it wouldn’t be available, is super early in files such as wp-config.php, and some drop-ins. But by the time an mu-plugin, a theme, or a plugin is loaded it’s present.

Now, it would work to wrap the inner workings of each function in a conditional, but could I wrap the add_action() statements in if( is_admin() ) instead, and keep from bloating the site’s respective lists of filters?

Yes, but in the example you gave it’s easier to just hook into admin_init instead. Adding stuff to hooks isn’t expensive, it’s what happens when the hook fires and the function runs that’s expensive.

Some Extra Notes

I see a lot of plugins that consist of a class that is instantiated in the same file, eg:

This is bad practice, you’re right to question it, and should not be copied. Do not create an object in the same file that its class is defined, it brings with it problems.

function __construct() {
  add_action('init', array( __CLASS__, 'baz' ) ); 
}

This means the moment you create this object, it will register hooks and start doing things. This can be annoying or create problems that are easily avoided by having a run or start function. It also makes writing unit tests very difficult, and in some cases impossible. A constructor should prepare the object for work, but it shouldn’t do the work. Think of it as buying a washing machine vs washing clothes with it. A constructor might assemble the machine and prep it ready, but it’s handy to be able to say when and if it runs

   add_action('init', array( __CLASS__, 'baz' ) ); 
 }

 static function baz() {

Because the function bar is static, if more than one of this object were created, baz would run multiple times

new My_Plugin();

There’s a few issues here:

  • Loading the file immediately creates an object and does stuff. You might not want it to do that stuff yet, so you’ve lost control of that
  • The object that gets created is never assigned to anything, it’s just left dangling. This makes stack traces and debugging significantly harder to do because it’s now an anonymous class
  • Because it’s anonymous, you can never unhook any of it’s actions. To do that you need a reference to the object, but that’s impossible here

The TLDR of the original answer though is that you can rely on is_admin being available, and you should never encounter a situation in a plugin or theme where it’s unavailable