Safe Function Call

Descripción

Safely call a function, class method, or object method in a manner that doesn’t generate errors if those plugins cease to exist.

Various helper functions are provided that provide handy variations of this theme:

  • _sfc(): Safely call a function and get its return value
  • _sfce(): Safely call a function and echo its return value
  • _sfcf(): Safely call a function; if it doesn’t exist, then a fallback function (if specified) is called
  • _sfcm(): Safely call a function; if it doesn’t exist, then echo a message (if provided)

Let’s assume 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 | Plugin Directory Page | GitHub | Author Homepage

Etiquetas de la plantilla

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.

Argumentos

  • $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.

Ejemplos

  • <?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');
    ?>

Instalación

  1. Install via the built-in WordPress plugin installer. Or download and unzip safe-function-call.zip inside the plugins directory for your site (typically wp-content/plugins/)
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  3. Use any of the four functions provided by this plugin as desired

Preguntas frecuentes

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

No

Why would I use any of these functions instead of using `function_exists()`/`method_exists()` directly?

The functions provided by this plugin provide a more concise syntax for checking for function existence (but they do use function_exists()/method_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 callback to be safely called is passed as an argument, it can be easily and more concisely parameterized.

Does this plugin include unit tests?

Yes.

Reseñas

enero 11, 2017
Works perfectly and offers enough fallback context to be very useful in theme development, especially themes or plugins which are designed for developers to extend. Thank you.
Leer la 1 reseña

Colaboradores y desarrolladores

“Safe Function Call” es un software de código abierto. Las siguientes personas han colaborado con este plugin.

Colaboradores

Traduce “Safe Function Call” a tu idioma.

¿Interesado en el desarrollo?

Revisa el código , echa un vistazo al repositorio SVN o suscríbete al registro de desarrollo por RSS.

Registro de cambios

1.2.8 (2019-06-09)

  • Change: Update unit test install script and bootstrap to use latest WP unit test repo
  • Change: Note compatibility through WP 5.2+

1.2.7 (2019-02-06)

  • New: Add CHANGELOG.md file and move all but most recent changelog entries into it
  • README.md:
    • New: Add instructions for use as a general PHP library
    • New: Add link to plugin’s WordPress Plugin Directory page
    • Change: Split paragraph in README.md’s «Support» section into two
  • Change: Note compatibility through WP 5.1+
  • Change: Update copyright date (2019)
  • Change: Update License URI to be HTTPS

1.2.6 (2018-01-02)

  • New: Add README.md
  • Change: Remove check that prevented use if ABSPATH isn’t defined, allowing use of the code outside WordPress
  • Change: Unit tests: Minor whitespace changes
  • Change: Add GitHub link to readme
  • Change: Note compatibility through WP 4.9+
  • Change: Update copyright date (2018)
  • Change: Unit tests: Add comments to denote groupings of unit tests testing a given function

Full changelog is available in CHANGELOG.md.