coffee2code

Safe Function Call

Author:Scott Reilly
Version:1.0
Last update:04 April 2008
Compatibility:WP 1.5+, 2.0+, 2.1+, 2.2+, 2.3+, and 2.5+
Comments:go here
Download:[ zip ]
Description:
Safely call functions that may not be available (for example, from within a template, calling a function that is provided by a plugin that may be deactivated).

Extended Description

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 the equivalent of doing :

<?php if function_exists('largest_city') { $value = largest_city('Tx'); echo $value; return $value; } ?>


Installation

Download the file safe-function-call.zip and unzip it into your wp-content/plugins/ directory. Activate the plugin through the ‘Plugins’ admin menu in WordPress. Use any of the three functions (_sfc(), _sfce(), or _sfcm()) provided by this plugin as desired.

Template Tags

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

Functions

<?php function _sfc($function_name) ?>

This will safely invoke the function by the name of `$function_name`. You can specify an arbitrary number of additional arguments that will get passed to `$function_name()`. If `$function_name()` does not exist, nothing is displayed and no error is generated.

<?php function _sfce($function_name) ?>

The same as _sfc() except that it echoes the return value of $function_name() before returning that value.

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

Arguments

$function_name

A string representing the name of the function to be called.

$message_if_missing

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

Examples

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

“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”

Frequently Asked Questions

Q: Do the functions provided by this plugin capture any error messages generated by the specified function?

A: No.


Release Log

04 Apr 2008 : v1.0 - Release notes

Copyright & Disclaimer

Copyright © 2007-2008 by Scott Reilly (aka coffee2code)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Acknowledgements

Thanks to all those who have contributed feedback and support!


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser