0

I just ran into an annoying problem on one of my WP sites. I'm using the qtranslate plugin and in the functions.php of my theme, I access some of that plugin's functions.

Now, it seems that functions.php of the activated theme is included in the WP backend (for some reason). This means that when I deactivate the plugin, the functions from that plugin which are used in functions.php causes not only the frontend, but also the backend to break

Fatal error: undefined function the_function.

This causes a tricky situation, as you cannot reactivate the plugin in the broken backend. My first thought was to manually define the functions I access and upload the functions.php via FTP. That works, but then you cannot activate the plugin because some of it's functions are already defined.

The only way to solve this (that I found), is to rename your functions.php, reactivate the plugin, then rename functions.php back.

Things brings me to my question (sorry for the long intro): what's the designated way to access plugin functions in a custom theme? Using a function_exists('the_function') everywhere isn't very handy. Does WP provide some sort of wrapper for this? Or am I looking at this entirely the wrong way?

  • 1
    If there are filters or actions then that would be the best way to go. That way you are 'hooking' into their code and not just directly calling their functions or redefining them. – Nick Young May 15 '14 at 7:18
  • This is just a thought, haven't tried this before, but I'm sure this is something that one could get working. Create a class and add all the relevant code from the plugin into that class. Then, register that class conditionally, like you said, if(function_exists( 'function_name' )) :. – Pieter Goosen May 15 '14 at 7:29
  • Nick: I don't think there are any in this case, but thanks. – Christof May 15 '14 at 8:29
  • Pieter: I was thinking something like that too, but I was hoping I wouldn't have to wrap every plugin with my own code. It would be nice if WP provided something like call_plugin('function_name', array('array', 'of', 'arguments')). Maybe I'll write something of the sort next time I need it. – Christof May 15 '14 at 8:31
  • You can have a look at is_plugin_active – Pieter Goosen May 15 '14 at 8:36
2

The active theme's functions.php is included on the backend because a theme can be configurable from the backend.

If your theme depends on the functions of a plug-in being available, then you must either make sure the plug-in is always available or make your theme robust enough to handle situations where it isn't.

You could put all of your theme's functions that need the plug-in's functions into a separate php file that is only included when the plug-in is active. This would make it easier for you to deactivate the plug-in for short periods while you make other updates.

So in your theme's functions.php you could add:

if (function_exists('plugin-function-name'))
{
    include 'path/to/functions-that-need-the-plugin.php';
}

Then you only have check if function exists once, and the theme and backend will still function when the plugin is not available.

  • Thanks for your answer. I had the same idea, but the theme's code would still break if you call your own functions that are only included if the plugin is active. – Christof May 15 '14 at 8:29
  • Yep. Dependency management is a true PITA in software development. – Todd Rowan May 15 '14 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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