WordPress.org

Ready to get started?Download WordPress

Plugin Directory

Safe Function Call

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

Template Tags

The plugin provides four functions for your use. Note: These functions are not limited to use in templates

Functions

  • <?php function _sfc($callback) ?> This will safely invoke the specified callback. You can specify an arbitrary number of additional arguments that will get passed to it. If the callback does not exist, nothing is displayed and no error is generated.

  • <?php function _sfce($callback) ?> The same as _sfc() except that it echoes the return value of the callback before returning that value.

  • <?php function _sfcf($callback, $fallback_callback = '') ?> The same as _sfc() except that it invokes the fallback callback (if it exists) if the callback does not exist. $function_name_if_missing() is sent $function_name as its first argument, and then subsequently all arguments that would have otherwise been sent to $function_name().

  • <?php function _sfcm($callback, $message_if_missing = '') ?> The same as _sfc() except that it displays a message (the value of $message_if_missing), if the callback does not exist.

Arguments

  • $callback A string representing the name of the function to be called, or an array of a class or object and its method (as can be done for add_action()/add_filter())

  • $message_if_missing (For _sfcm() only.) The message to be displayed if $function_name() does not exist as a function.

  • $fallback_callback (For _sfcf() only.) The function to be called if the callback does not exist.

Examples

  • <?php _sfc('list_cities', 'Texas', 3); /* Assuming list_cities() is a valid function */ ?> "Austin, Dallas, Fort Worth"

  • <?php _sfc(array('Cities', 'list_cities'), 'Texas', 3); /* Assuming list_cities() is a valid function in the 'Cities' class */ ?> "Austin, Dallas, Fort Worth"

  • <?php _sfc(array($obj, 'list_cities'), 'Texas', 3); /* Assuming list_cities() is a valid function in the object $obj */ ?> "Austin, Dallas, Fort Worth"

  • <?php _sfc('list_cities', 'Texas', 3); /* Assuming list_cities() is not a valid function */ ?> ""

  • <?php _sfcm('list_cities', 'Texas', 'Unable to list cities at the moment', 3); /* Assuming list_cities() is a valid function */ ?> "Austin, Dallas, Fort Worth"

  • <?php _sfcm('list_cities', 'Texas', 'Unable to list cities at the moment', 3); /* Assuming list_cities() is not a valid function */ ?> "Unable to list cities at the moment"

  • <?php _sfce('largest_city', 'Tx'); /* Assuming largest_city() is a valid function that does not echo/display its return value */ ?> "Houston"

  • <?php function unavailable_function_handler( $callback ) { echo "Sorry, but the function {$callback}() does not exist."; } _sfcf('nonexistent_function', 'unavailable_function_handler'); ?>

Requires: 1.5 or higher
Compatible up to: 3.8.3
Last Updated: 2013-12-19
Downloads: 1,024

Ratings

3 stars
3.5 out of 5 stars

Support

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

100,1,1
100,1,1
100,1,1
100,1,1
100,1,1
100,1,1