Title: Safe Function Call
Author: Scott Reilly
Published: <strong>Òkúdù 11, 2009</strong>
Last modified: Igbe 14, 2025

---

Ṣàwárí àwọn plugin

![](https://ps.w.org/safe-function-call/assets/banner-772x250.png?rev=825360)

![](https://ps.w.org/safe-function-call/assets/icon-128x128.png?rev=972887)

# Safe Function Call

 Láti ọwọ́ [Scott Reilly](https://profiles.wordpress.org/coffee2code/)

[Ṣe ìgbàsílẹ̀](https://downloads.wordpress.org/plugin/safe-function-call.1.4.zip)

 * [Àwọn àlàyé](https://yor.wordpress.org/plugins/safe-function-call/#description)
 * [Àwọn àgbéyẹ̀wò](https://yor.wordpress.org/plugins/safe-function-call/#reviews)
 *  [Ìgbéwọlẹ̀](https://yor.wordpress.org/plugins/safe-function-call/#installation)
 * [Ìdàgbàsókè](https://yor.wordpress.org/plugins/safe-function-call/#developers)

 [Ìrànlọ́wọ́](https://wordpress.org/support/plugin/safe-function-call/)

## Àpèjúwe

Safely call a function, class method, or object method in a manner that doesnÌtumọ̀
Yorùbá: ’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Ìtumọ̀ Yorùbá: ’t exist, then a
   fallback function (if specified) is called
 * `_sfcm()`: Safely call a function; if it doesnÌtumọ̀ Yorùbá: ’t exist, then echo
   a message (if provided)

LetÌtumọ̀ Yorùbá: ’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Ìtumọ̀ Yorùbá: ’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Ìtumọ̀ Yorùbá: ’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' ); } ?>
    ```

#### Filter invocation method

To further prevent issues in your code should this plugin itself become deactivated,
you can use indirect filter invocation to call the plugin functions. Each function
has an associated filter with the same name as the function. Simply use `apply_filters()`
to invoke that function instead of calling the function directly.

E.g. instead of:

    ```
    <?php _sfce( 'some_plugin_function_that_echoes', 'argument' ); ?>
    ```

Do:

    ```
    <?php apply_filters( '_sfce', 'some_plugin_function_that_echoes', 'argument' ); ?>
    ```

If youÌtumọ̀ Yorùbá: ’re relying on the return value of a function and this plugin
gets deactivated, note that the `apply_filters()` call will return the name of the
function you intended to call, so you should check the return value to ensure the
function got called.

Instead of:

    ```
    <?php $x = _sfc( 'some_plugin_function', 'argument' ); ?>
    ```

Do:

    ```
    <?php
        $x = apply_filters( '_sfcq', 'some_plugin_function', 'argument' );
        if ( $x !== 'some_plugin_function' ) {
            // Work with the value of $x here.
        } else {
            // The Safe Function Call plugin isn't active.
            $x = 0; // Maybe set the variable to something that makes sense in this scenario.
        }
    ?>
    ```

Links: [Plugin Homepage](https://coffee2code.com/wp-plugins/safe-function-call/)
| [Plugin Directory Page](https://wordpress.org/plugins/safe-function-call/) | [GitHub](https://github.com/coffee2code/safe-function-call/)
| [Author Homepage](https://coffee2code.com)

### Developer Documentation

Developer documentation can be found in [DEVELOPER-DOCS.md](https://github.com/coffee2code/safe-function-call/blob/master/DEVELOPER-DOCS.md).
That documentation covers the template tags and hooks provided by the plugin.

As an overview, these are the template tags provided by the plugin:

 * `_sfc()` : Safely call a function and get its return value.
 * `_sfce()` : Safely call a function and echo its sanitized return value.
 * `_sfcf()` : Safely call a function; if it doesnÌtumọ̀ Yorùbá: ’t exist, then 
   a fallback function (if specified) is called.
 * `_sfcm()` : Safely call a function; if it doesnÌtumọ̀ Yorùbá: ’t exist, then 
   echo a message (if provided).

These are the hooks provided by the plugin. They are intended for filter invocation
usage rather than typical content filtering.

 * `_sfc` : Filter to safely invoke `_sfc()` in such a way that if the plugin were
   deactivated or deleted, then your calls to the function wonÌtumọ̀ Yorùbá: ’t 
   cause errors in your site.
 * `_sfce` : Filter to safely invoke `_sfce()` in such a way that if the plugin 
   were deactivated or deleted, then your calls to the function wonÌtumọ̀ Yorùbá:’
   t cause errors in your site.
 * `_sfcf` : Filter to safely invoke `_sfcf()` in such a way that if the plugin 
   were deactivated or deleted, then your calls to the function wonÌtumọ̀ Yorùbá:’
   t cause errors in your site.
 * `_sfcm` : Filter to safely invoke `_sfcm()` in such a way that if the plugin 
   were deactivated or deleted, then your calls to the function wonÌtumọ̀ Yorùbá:’
   t cause errors in your site.

## Ìgbéwọlẹ̀

 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

## FAQ

### 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.

### WonÌtumọ̀ Yorùbá: ’t the problems this plugin addresses become a problem when using this plugin if it itself gets deactivated?

Yes, if you make direct use of one of this pluginÌtumọ̀ Yorùbá: ’s functions and
then deactivate the plugin, you will likely encounter an error.

However, if you make use indirect filter invocation, you can prevent errors. See
the “Filter invocation method” section of the extended plugin description for example
code.

### Does this plugin include unit tests?

Yes. The tests are not packaged in the release .zip file or included in plugins.
svn.wordpress.org, but can be found in the [pluginÌtumọ̀ Yorùbá: ’s GitHub repository](https://github.com/coffee2code/safe-function-call/).

## Àwọn àgbéyẹ̀wò

![](https://secure.gravatar.com/avatar/b553ddfd3593f64cda4b436e9f839c31c6cf29fea457be2a42b64b6373252ec6?
s=60&d=retro&r=g)

### 󠀁[Works perfectly.](https://wordpress.org/support/topic/works-perfectly-1014/)󠁿

 [Noahj Champion](https://profiles.wordpress.org/noahj-champion/) Ṣẹrẹ 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.

 [ Ka gbogbo àgbéyẹ̀wò 1 ](https://wordpress.org/support/plugin/safe-function-call/reviews/)

## Àwọn Olùkópa & Olùgbéejáde

“Safe Function Call” jẹ́ ètò ìṣàmúlò orísun ṣíṣí sílẹ̀. Àwọn ènìyàn wọ̀nyí ti ṣe
ìkópa sí plugin yìí.

Àwọn Olùkópa

 *   [ Scott Reilly ](https://profiles.wordpress.org/coffee2code/)

[Túmọ̀ “Safe Function Call” sí èdè rẹ.](https://translate.wordpress.org/projects/wp-plugins/safe-function-call)

### Ṣe o nífẹ̀ẹ́ sí ìdàgbàsókè?

[Ṣàwárí koodu](https://plugins.trac.wordpress.org/browser/safe-function-call/), 
ṣàyẹ̀wò [ibi ìpamọ́ SVN](https://plugins.svn.wordpress.org/safe-function-call/),
tàbí ṣe àgbékalẹ̀ sí [àkọsílẹ̀ ìdàgbàsókè](https://plugins.trac.wordpress.org/log/safe-function-call/)
nípasẹ̀ [RSS](https://plugins.trac.wordpress.org/log/safe-function-call/?limit=100&mode=stop_on_copy&format=rss).

## Àkọsílẹ̀ àwọn àyípadà

#### 1.4 (2025-04-09)

Highlights:

This minor release sanitizes the output of the callback return value in `_sfce()`
and the `$msg_if_missing` argument value to `_sfce()`, notes compatibility through
WP 6.8+ and PHP 8.3+, and updates copyright date (2025).

Details:

 * Hardening: Sanitize output of callback return value in `_sfce()`
 * Hardening: Sanitize output of `$msg_if_missing` argument value to `_sfcm()`
 * Change: Note compatibility through WP 6.8+
 * Change: Note compatibility through PHP 8.3+
 * Change: Update copyright date (2025)

#### 1.3.3 (2024-08-09)

 * Change: Note compatibility through WP 6.6+
 * Change: Update copyright date (2024)
 * New: Add `.gitignore` file
 * Change: Remove development and testing-related files from release packaging
 * Unit tests:
    - Hardening: Prevent direct web access to `bootstrap.php`
    - Allow tests to run against current versions of WordPress
    - New: Add `composer.json` for PHPUnit Polyfill dependency
    - Change: In bootstrap, store path to plugin directory in a constant

#### 1.3.2 (2023-05-19)

 * New: Add DEVELOPER-DOCS.md and move hooks documentation into it
 * New: Add TODO.md with potential TODO items
 * Change: Improve some inline documentation formatting
 * Change: Note compatibility through WP 6.3+
 * Change: Update copyright date (2023)

#### 1.3.1 (2021-09-26)

 * Change: Note compatibility through WP 5.8+
 * Unit tests:
    - Change: Restructure unit test directories
       * Change: Move `phpunit/bin/` into `tests/`
       * Change: Move `phpunit/` into `tests/`
    - Change: Remove ‘test-‘ prefix from unit test file
    - Change: In bootstrap, store path to plugin file constant
    - Change: In bootstrap, add backcompat for PHPUnit pre-v6.0

_Full changelog is available in [CHANGELOG.md](https://github.com/coffee2code/safe-function-call/blob/master/CHANGELOG.md)._

## Àkójọpọ̀ Meta

 *  Ẹ̀yà **1.4**
 *  Ìgbàgbọ́hùn tó kẹ́yìn **ọdún 1 sẹ́yìn**
 *  Àwọn ìgbéwọlẹ̀ tó ṣiṣẹ́ **10+**
 *  Ẹ̀yà WordPress ** 1.5 tàbí ju bẹ́ẹ̀ lọ **
 *  Dánwò dé **6.8.5**
 *  Èdè
 * [English (US)](https://wordpress.org/plugins/safe-function-call/)
 * Àwọn àmì
 * [coffee2code](https://yor.wordpress.org/plugins/tags/coffee2code/)[error](https://yor.wordpress.org/plugins/tags/error/)
   [function](https://yor.wordpress.org/plugins/tags/function/)[template](https://yor.wordpress.org/plugins/tags/template/)
 *  [Ìwòye Tó Péye](https://yor.wordpress.org/plugins/safe-function-call/advanced/)

## Àwọn ìbò

 5 lára àwọn ìràwọ̀ 5.

 *  [  1 5-star review     ](https://wordpress.org/support/plugin/safe-function-call/reviews/?filter=5)
 *  [  0 4-star reviews     ](https://wordpress.org/support/plugin/safe-function-call/reviews/?filter=4)
 *  [  0 3-star reviews     ](https://wordpress.org/support/plugin/safe-function-call/reviews/?filter=3)
 *  [  0 2-star reviews     ](https://wordpress.org/support/plugin/safe-function-call/reviews/?filter=2)
 *  [  0 1-star reviews     ](https://wordpress.org/support/plugin/safe-function-call/reviews/?filter=1)

[Your review](https://wordpress.org/support/plugin/safe-function-call/reviews/#new-post)

[See all reviews](https://wordpress.org/support/plugin/safe-function-call/reviews/)

## Àwọn Olùkópa

 *   [ Scott Reilly ](https://profiles.wordpress.org/coffee2code/)

## Ìrànlọ́wọ́

Nǹkan wà tí o fẹ́ sọ? Ṣé o nílò ìrànlọ́wọ́?

 [Wo àpéjọ ìrànlọ́wọ́](https://wordpress.org/support/plugin/safe-function-call/)

## Ṣe ìtọrẹ

Ṣé o fẹ́ ṣe àtìlẹ́yìn fún ìlọsíwájú plugin yìí?

 [ Ṣe ìtọrẹ sí plugin yìí ](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6ARCFJ9TX3522)