Check and load class of theme from a plugin

I’d recommend to not use methods from your theme in the plugins. Only the other way around. Let your plugins provide methods to be used in the theme. Then you could simply use is_plugin_active().


Nonetheless you could use PHP’s class_exists() to check for the existence of a class. Another common practice seems to be to let your plugin die; at the very top before anything else can be loaded.

<?php
/*
Plugin Name: My Plugin
Description: Lorem ipsum dolor sit amet.
Version: 1.0
Author: You
Author URI: https://example.com
*/

if ( ! class_exists('MyThemeClass') ) {
  die;
}

class MyPlugin() {
  // ...
}

new MyPlugin;