I've set up a class for my function, and I'd like to implement some form of error handling. But I'm new to this error handling lark.

Can I set_error_handler to only show certain messages (and can these be as wordpress alerts in the admin section)?

And if there is a fatal error in my plugin (there are none on my dev set up, but I'm trying to catch anything that might occur on a live server), is there a way to catch this to avoid a site crash? Or white screen of death?

I'm thinking it must be possible on a fatal error (or anything else that might ruin the site), to automagically deactivate the plugin? and send a wp alert message.

Am I on the right track? Does anyone have an example they could show me?

  • Fatal errors cannot be caught with set_error_handler(). And I don't see the reason why you would want to do that. You should not get such errors unless there's something wrong in your plugin. Dec 22, 2011 at 14:26

1 Answer 1


Fatal errors point to bad syntax, or bad naming conventions. WordPress does a good job of not activating plugins that throw fatal errors. However, once a plugin is activated, all bets are off. If you've created a function or a class, it's best to follow good naming conventions and namespace properly.

For example, a function called post_extras() would not be a good name as it most likely would conflict with another badly named function down the road from another plugin. Select a namespace prefix for each plugin that is unique to the plugin itself such as, foobar123_post_extras().

When asking people to embed template tags for your plugin, you should isolate those functions.

      echo foobar123_post_extras();

When declaring classes or functions isolate them this way:

     function foobar123_post_extras(){
             //do something

      class FooBar123Class{

This is a best practice for guarding against fatal errors with function and class name conflicts.

If you are including any other file within your plugins, you can certainly minus fatal errors by using the include functions rather than require functions. Check out this article for more information. You could deactivate the plugin if your includes fail like this:

     $plugins = get_option('active_plugins');
     $index = array_search(plugin_basename(__FILE__), $plugins);
          update_option('active_plugins', $plugins);

WordPress has a good document for coding standards here regarding syntax styling.

Make sure you aren't using php short tags as they will cause errors on servers that don't have short tags enabled.

<?= - short tag
<?php echo - proper

For database concerns, always use the $wpdb class when querying the database. You cannot count on users to have the same db prefix that you use.

global $wpdb;
$results = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' LIMIT 5");

Finally, before releasing a plugin, it's importing to throw the WP_DEBUG switch in your local environment to see if you are using deprecated WordPress functions or causing other conflicts. If a function disappears in a future release, it certainly will cause fatal problems with your plugin. To use WP_DEBUG, simply add this to your wp-config.php file:

define('WP_DEBUG', true);

Hope this helps give you some clues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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