Plugin Directory

Safe Function Call

Author: Scott Reilly

Safely and easily call functions that may not be available (such as those provided by a plugin that gets deactivated).

Assuming you had something like this in a template:

<?php list_cities('Texas', 3); ?>

If you deactivated the plugin that provided list_cities(), your site would generate an error when that template is accessed.

You can instead use _sfc(), which is provided by this plugin to call other functions, like so:

<?php _sfc('list_cities', 'Texas', 3); ?>

That will simply do nothing if the list_cities() function is not available.

If you'd rather display a message when the function does not exist, use _sfcm() instead, like so:

<?php _sfcm('list_cities', 'The cities listing is temporarily disabled.', 'Texas', 3); ?>

In this case, if list_cities() is not available, the text "The cities listing is temporarily disabled." will be displayed.

In the event you want to safely call a function and echo its value, you can use _sfce() like so:

<?php _sfce('largest_city', 'Tx'); ?>

Which is roughly equivalent to doing :

<?php if function_exists('largest_city') { echo largest_city('Tx'); } ?>

Download

FYI

Compatibility beta

Your Setup

Log in to vote.

The Consensus

No data

Average Rating

5 stars
4 stars
3 stars
2 stars
1 star
(0 ratings)