Safe Function Call

Safe Function Call

Wordpress plugin

Install on Wordpress

App Details

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

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’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 | 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 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).

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

Pricing

Starting from $0 per month.

Check Out the Pomodoro Timer Widget

By Common Ninja

Pomodoro TimerTry For Free!

App Info

Rating

Reviewers

1 reviews

Tags

coffee2code
error
function
template

Developed By

Scott Reilly

Quick & Easy

Find the Best Wordpress plugins for you

Common Ninja has a large selection of powerful Wordpress plugins that are easy to use, fully customizable, mobile-friendly and rich with features — so be sure to check them out!

Testimonial

Testimonial plugins for Wordpress

Galleries

Galleries plugins for Wordpress

SEO

SEO plugins for Wordpress

Contact Form

Contact Form plugins for Wordpress

Forms

Forms plugins for Wordpress

Social Feeds

Social Feeds plugins for Wordpress

Social Sharing

Social Sharing plugins for Wordpress

Events Calendar

Events Calendar plugins for Wordpress

Sliders

Sliders plugins for Wordpress

Analytics

Analytics plugins for Wordpress

Reviews

Reviews plugins for Wordpress

Comments

Comments plugins for Wordpress

Portfolio

Portfolio plugins for Wordpress

Maps

Maps plugins for Wordpress

Security

Security plugins for Wordpress

Translation

Translation plugins for Wordpress

Ads

Ads plugins for Wordpress

Video Player

Video Player plugins for Wordpress

Music Player

Music Player plugins for Wordpress

Backup

Backup plugins for Wordpress

Privacy

Privacy plugins for Wordpress

Optimize

Optimize plugins for Wordpress

Chat

Chat plugins for Wordpress

Countdown

Countdown plugins for Wordpress

Email Marketing

Email Marketing plugins for Wordpress

Tabs

Tabs plugins for Wordpress

Membership

Membership plugins for Wordpress

popup

popup plugins for Wordpress

SiteMap

SiteMap plugins for Wordpress

Payment

Payment plugins for Wordpress

Coming Soon

Coming Soon plugins for Wordpress

Ecommerce

Ecommerce plugins for Wordpress

Customer Support

Customer Support plugins for Wordpress

Inventory

Inventory plugins for Wordpress

Video Player

Video Player plugins for Wordpress

Testimonials

Testimonials plugins for Wordpress

Tabs

Tabs plugins for Wordpress

Social Sharing

Social Sharing plugins for Wordpress

Social Feeds

Social Feeds plugins for Wordpress

Slider

Slider plugins for Wordpress

Reviews

Reviews plugins for Wordpress

Portfolio

Portfolio plugins for Wordpress

Membership

Membership plugins for Wordpress

Forms

Forms plugins for Wordpress

Events Calendar

Events Calendar plugins for Wordpress

Contact

Contact plugins for Wordpress

Comments

Comments plugins for Wordpress

Analytics

Analytics plugins for Wordpress

Common Ninja Apps

Some of the best Common Ninja plugins for Wordpress

Browse our extensive collection of compatible plugins, and easily embed them on any website, blog, online store, e-commerce platform, or site builder.

Pomodoro Timer for Wordpress logo

Pomodoro Timer

Add a Pomodoro timer to your site so users can structure focus and break cycles, improving time management and productivity.

Vimeo Slider for Wordpress logo

Vimeo Slider

Show Vimeo videos with a Vimeo slider that displays clips in a smooth slider to boost engagement and keep visitors watching.

Countdown for Wordpress logo

Countdown

Add a countdown timer to your site to highlight upcoming events or launches and encourage timely user action.

Yelp Reviews for Wordpress logo

Yelp Reviews

Show Yelp reviews to build trust, strengthen credibility, and help visitors make confident decisions that support higher sales.

Audio Player for Wordpress logo

Audio Player

Add a no-code audio player to your site and boost engagement with music, podcasts, and voice content effortlessly.

Scroll to Element Button for Wordpress logo

Scroll to Element Button

Scroll to element button that improves navigation by letting visitors jump directly to key sections, reducing friction and boosting overall engagement.

eBay Reviews for Wordpress logo

eBay Reviews

Show eBay reviews to build trust, strengthen credibility, and help visitors make confident purchase decisions that support higher sales.

Corner Coupon Pop-up for Wordpress logo

Corner Coupon Pop-up

Add a corner coupon pop-up to highlight discounts, collect emails, and drive user engagement without interrupting browsing.

Back to Top Button for Wordpress logo

Back to Top Button

Back to top button that improves navigation, speeds up browsing, and helps visitors move smoothly through long pages for a better user experience.

Tilted Image for Wordpress logo

Tilted Image

Use tilted image effects to rotate visuals, add creative style, and keep visitors engaged with dynamic images on your site.

Image Poll for Wordpress logo

Image Poll

Create interactive image polls that use compelling visuals to boost engagement, gather feedback, and help visitors vote easily.

Twitch Feed for Wordpress logo

Twitch Feed

Show live streams and clips with a Twitch feed that keeps content fresh, boosts engagement, and helps visitors follow your channel more easily.

Discover Apps By Platform

Discover the best apps for your website

WordPress
Wix
Shopify
Weebly
Webflow
Joomla
PrestaShop
Shift4Shop
WebsiteX5
MODX
Opencart
NopCommerce

Common Ninja Search Engine

The Common Ninja Search Engine platform helps website builders find the best site widgets, apps, plugins, tools, add-ons, and extensions! Compatible with all major website building platforms - big or small - and updated regularly, our Search Engine tool provides you with the business tools your site needs!

Multiple platforms