Building a plugin that doesn’t break when deactivated

I wrote a WordPress plugin that creates an admin page for deciding what posts go in a feature slider on the front page. Everything works fine and I’m simply calling to the function inside the plugin with

    <?php uwffs_display(); ?> 

in the home.php file.

Read More

The issue is that if I were to deactivate the plugin, the home page breaks at the point the

    <?php uwffs_display(); ?> 

function call occurs and stops rendering the rest of the page.

What is a more graceful way I can call this plugin’s function so that, if deactivated, it will load the rest of the page?

One thought is I could write it as:

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

Is that the best way?

Related posts

Leave a Reply

2 comments

  1. The other option would be to use any hooks available in the Theme, which would allow your Plugin to inject the slider at a filter or action hook. If the Plugin is deactivated, its add_action() or add_filter() call is never run, nothing attempts to be added to the template, and, voila: no breakage.

    But, barring that (and since Themes that offer such custom hooks are still in the minority), function_exists() wrapper is the way to go. So +1 to @rmlumley.