Safe Function Call
- Author:Scott Reilly
- Version:1.1.7
- Last update:2012-06-04
- Compatibility:WP 1.5 – 3.5.1
- Comments:go here
- Download:[ zip ]
- Description:
Safely and easily call functions that may not be available (such as those provided by a plugin that gets deactivated).
-
Extended Description
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.If you’d rather call another function when the function does not exist, use _sfcf() instead, like so:
<?php function unavailable_function_handler( $function_name ) { echo "The function $function_name is not available."; } _sfcf( 'nonexistent_function', 'unavailable_function_handler' ); ?>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' ); } ?>Links: Plugin Homepage | Author Homepage
Find out more at the plugin’s WordPress Plugin Repository page.
Installation
- Unzip
safe-function-call.zipinside the/wp-content/plugins/directory (or install via the built-in WordPress plugin installer) - Activate the plugin through the ‘Plugins’ admin menu in WordPress
- Use any of the four functions provided by this plugin as desired
- Unzip
-
Template Tags
The plugin provides four 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 _sfcf($function_name, $function_name_if_missing = '') ?>
The same as_sfc()except that it invokes$function_name_if_missing()(if it exists), if$function_name()does not exist.$function_name_if_missing()is sent$function_nameas its first argument, and then subsequently all arguments that would have otherwise been sent to$function_name(). -
<?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. -
$function_name_if_missing
(For_sfcf()only.) The function to be called 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” -
<?php
function unavailable_function_handler( $function_name ) {
echo "Sorry, but the function {$function_name}() does not exist.";
}
_sfcf('nonexistent_function', 'unavailable_function_handler');
?>
-
Frequently Asked Questions
Q. Do the functions provided by this plugin capture any error messages generated by the specified function?
A. No.Q. Why would I use any of these functions instead of checking if `function_exists()` directly?
A. The functions provided by this plugin provide a more concise syntax for checking for function existence (usingfunction_exists()under the hood)._sfce()will both echo and return the echoed value, which may be of use in certain circumstances. And also, since the name of the function to be safely called is passed as an argument, it can be easily and more concisely parameterized.
Release Log
1.1.7
- Note compatibility through WP 3.5+
- Update copyright date (2013)
1.1.6
- Re-license as GPLv2 or later (from X11)
- Add ‘License’ and ‘License URI’ header tags to readme.txt and plugin file
- Remove ending PHP close tag
- Miscellaneous readme.txt changes
- Update copyright date (2012)
- Note compatibility through WP 3.4+
1.1.5
- Note compatibility through WP 3.3+
- Minor code documentation reformatting in readme.txt (spacing)
1.1.4
- Note compatibility through WP 3.2+
- Minor documentation reformatting in readme.txt
- Fix plugin homepage and author links in description in readme.txt
1.1.3
- Add link to plugin homepage to readme.txt
1.1.2
- Note compatibility through WP 3.1+
- Update copyright date (2011)
1.1.1
- Wrapped functions in if(function_exists()) checks
- Note compatibility with WP 3.0+
- Change description
- Minor code reformatting (spacing)
- Remove documentation and instructions from top of plugin file (all of that and more are contained in readme.txt)
- Add Upgrade Notice section to readme.txt
1.1
- Add new template function _sfcf() to allow calling a function when the intended function isn’t available
- Add PHPDoc documentation
- Minor formatting tweaks
- Note compatibility with WP 2.9+
- Update copyright date
- Update readme.txt (including adding Changelog)
1.0
- Initial release
Copyright & Disclaimer
Copyright © 2009-2012 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.
