The Correct Way To Add Plugin’s Functions Into Themes

I recently deactivated a plugin on this blog only to find that the blog stopped working. On the home page, only an error was displayed. The error told me that I was trying to call some function that didn’t exist. Clearly, the function disappeared because of the deactivation of plugin. To check my theory, I re-activated the plugin and the blog started working again.

The plugin I deactivated was related post plugin, and when I checked the theme files, there was a call to the function,

<?php related_posts(); ?>

I removed that function call and deactivated the plugin, which fixed the issue.

I am sure that you must have came across the same same issue in the past. Here’s a solution to the issue-

The Correct Way To Add Plugin’s Functions Into Themes

The correct way to call plugin’s function in a theme is this,

<?php if(function_exists('function_name'))
{
function_name();
}
?>

As you can see in the code snippet above, an if condition is used to check if a function exists. If it doesn’t, then the won’t be called, thus preventing the blog from breaking completely.

So, to call related_posts function, the following code should be added to the theme theme’s template files,

<?php if(function_exists('related_posts'))
{
related_posts();
}
?>

Now, even if the related posts plugin is deactivated, it won’t affect the blog.

If you are a WordPress theme developer and you want to integrate a plugin with a theme, or if you are a WordPress user who need to add a function call to theme files to use some plugin, then please add the function call in the correct way.

share on twitter

Leave a Reply