I'm in the process of breaking down a theme to a bunch of plugins, I wanna do it one by one so hell won't break loose.

Question is, Suppose I have a plugin which depends on a class from the theme, where and how should I validate his existence and load it, and if not exist how do I properly show an error and avoid loading the plugin, is it on the activation hook? I'm pretty new to plugins, and would love to get best practices, or any good docs references regarding my issue, thanks!

I don't need to check another plugins existence, just another code that is currently part of the theme.


1 Answer 1


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.

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

if ( ! class_exists('MyThemeClass') ) {

class MyPlugin() {
  // ...

new MyPlugin;
  • Regarding your recommendation, I totally agree but the general direction is to make the part of this theme code as a different plugin.. so in future the code will be loaded from another plugin, and I guess this is your suggestion, Commented Mar 28, 2019 at 8:35
  • @ChenKinnrot – Exactly! :)
    – leymannx
    Commented Mar 28, 2019 at 8:38
  • do I need to require_once the class file or if class exists it's already loaded ? Commented Mar 28, 2019 at 8:39
  • All you need would be is_plugin_active() and then just call MyPlugin::MyMethod(). No includes necessary. And as long as it's your own project and you are calling these methods only in your custom theme, you might as well skip is_plugin_active(). Alternatively you could let your theme class provide its own helper method(s) that checks all desired plugins for activeness and then returns the plugin's method output or FALSE otherwise.
    – leymannx
    Commented Mar 28, 2019 at 8:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.