Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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?

share|improve this question
add comment

migrated from stackoverflow.com Aug 16 '11 at 17:33

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
add comment

Yes it is. Checking for the existence of the function in the way that you just posted is the proper way of doing this.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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