I created a WordPress plugin using OOP. If the plugin hasn't been activated and a method is called in a theme file, it throws the PHP fatal error "call to undefined function".

What is the correct way of doing this in the event the plugin hasn't been enabled?

Should a check be made in every file where it's used within the theme directory to see if the class exists? Or should the error just be left as is and the user will know they have to enable the plugin based on the function name?

Here's what I would like to do:

if ( mypluginname_is_event_active() ) {
  echo 'it is!';

However, the code above triggers the PHP fatal error if the plugin hasn't been activated.

The code below prevents the error from appearing, but it seems really clunky. Especially if it has to be done in every file where it's used under themes:

// begin top of file
$is_event_live = false;

if ( class_exists( 'MyPluginName' ) ) {
  $is_event_live = mypluginname_is_event_active();
// end top of file

if ( $is_event_live ) {
  echo 'it is!';

2 Answers 2


A plugin is a PHP include in its most elementary sense. You cannot call a function if it's not registered in PHP. If a plugin is not activated, it is therefore not included, and the function is unknown. You cannot call it. I would recommend that you use this as a must-use plugin so that it's availability is consistent and predictable.


to link plugin and theme, I recommend you to use hooks.
In the theme, you create a new hook with do_action("under_the_picture");. And in the plugin you make a hook like this :

add_action("under_the_picture", function () {
    echo "plugin data to display under the picture";

then when the plugin is not activated, the theme place "under_the_picture" is just empty and there is no error.

  • Thanks for the tip. Can you give me an example of how this would be done in a case where I needed to check to see if an event is currently taking place. If it is, additional code needs to be displayed on the page. If it isn't, then the code should not be displayed.
    – user1462
    Aug 12, 2015 at 20:17
  • @user1462 you could look into adding the_content filter, and within the filter run your logic Aug 12, 2015 at 20:37
  • @czerspalace thanks for the tip. in this case, there's only three pages that need this code out of around a thousand, so I think that might be a bit overkill for my situation.
    – user1462
    Aug 12, 2015 at 20:52
  • I'm not sure how adding the filter is overkill, but adding the do_action to your themes files does seem like another good alternative Aug 12, 2015 at 23:51

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.