Sciencetext Tips & Tricks

Blogging tips, browsing tricks and computing hacks

The Secret of If and Else

February 26th, 2007 · by David Bradley

If you’re running plugins on your site that are called using PHP, then it’s always a good idea to embed the include in an “if-else” statement.

This condition will first check that the plugin is up and running and if it is, will run it as normal. But, if, say you disabled an errant plugin, without that if-else, you’ll simply get an error on the site telling readers that a plugin is missing, which you really don’t want them seeing, not least because it just looks plain amateurish, but also because it might offer hackers a clue as to how to break into your site.

Here’s how it works at its simplest:

<?php
if (function_exists(’your_plugin’)) {
your_plugin();
}
?>

This code will look for the plugin and run it if it finds it, if it doesn’t find it, it shouldn’t return an error, but simply continue rendering the site and perhaps leave a gap depending on your site’s cascading stylesheet (CSS). (You are using stylesheets for layout aren’t you?)

But, what about that “else”? What if you want something to happen if the plugin is missing? Just extend the code:

<?php
if (function_exists(’your_plugin’)) {
your_plugin();
}

else {
echo “The plugin is temporarily unavailable”;
}
?>

Let me know how you get on with this, it can save a lot of awkward script errors when you disable plugins for debugging and during upgrades.

0 responses so far ↓

  • Got something to say? Start a comment thread here. Top commentators get a FREE link on the homepage!

Leave a Comment

Comments are checked for spam before appearing, no need to post it twice.

Related Posts