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:
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:
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:
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:
In the event you want to safely call a function and echo its value, you can use _sfce() like so:
Which is roughly equivalent to doing :
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:
Do:
If you』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:
Do:
Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage
Developer Documentation
Developer documentation can be found in 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』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).
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』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』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』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』t cause errors in your site.






