Going down? if @rogieking wins, he's donating the prize to charity. Please vote #komodomedia here: http://is.gd/8duC 17 hrs ago
Tags: , , , ,

This is a quickie, but is dedicated to any budding WordPress theme and plugin developers who are just getting familiar with PHP.

If you make a call to a plugin function like, oh say, <?php wp_beta_test(); ?>, and that plugin is not currently activated in the WP backend, nothing below that line of code will load. This can be particularly mystifying if you make the call towards the top of your page like in the header. Viewing the source to see what loads is a good way to debug it, but here is a simple way to avoid that issue altogether.

PHP has a handy self-referential function called “function_exists.” It allows you to check if said function exists before running it (duh). By wrapping your plugin call in two lines of PHP, you can show the desired result when available, or discretely pass over the error if the function is absent.

For example, I’m working on a plugin that helps me beta test my new themes and plugins. Since it is under heavy development, it isn’t always activated, and quite often tries to kill my page load. I prevent that by calling it like this:

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

Piece of cake. Note that you don’t have to pass any of the plugin’s arguments (the stuff between the parentheses) in the first line of code. With a little creativity, you can also return a helpful message for yourself like “Hey, this plugin isn’t activated, Stupid!”

Leave a Reply