13

I want to prevent fatal error in my theme if the ACF plugin is deactivated or not installed.

The main function of the plugin is get_field(). I wrote this code in my functions.php to check:

if ( !function_exists('get_field') ) {
  function get_field() {
    echo '<span>plugin ACF is not installed</span>';
  }
}

Please tell me this is acceptable practice?

James Jones
  • 3,850
  • 5
  • 25
  • 44
Joe
  • 353
  • 1
  • 3
  • 12

4 Answers4

26

ACF itself uses a check to see if the framework has been loaded. If it has already been included and invoked by another plugin or theme, then ACF won't re-instantiate its own class again. It does this with a class check:

if (!class_exists('ACF')) {
    //  The ACF class doesn't exist, so you can probably redefine your functions here
}

I use exactly this in my own plugins that rely on the presence of ACF so that if it happens to get deactivated, the whole site doesn't bomb out.

indextwo
  • 5,535
  • 5
  • 48
  • 63
5

First of all, this is not the main plugin function, just one of them. Probably, most commonly used by a plugin user in a theme. Another one is the_field(), which actually prints value (get_field() returns it).

Regarding practice of defining your custom function - it's fine. However, I would not print that long message in every place where ACF field is expected - some of them may be short (numbers), and this message will break the layout. Printing something shorter is better, imo.

Also, function_exists is proper check, not is_plugin_active, because ACF can also be shipped as a library with a theme framework or other plugin.

Another option is to remove ACF dependency on the frontend completely. You can output the contents of the fields with get_post_meta() and prevent ACF plugin from loading on the frontend entirely. See these two posts for details:

http://www.billerickson.net/code/disable-acf-frontend/

http://www.billerickson.net/advanced-custom-fields-frontend-dependency/

Ihor Vorotnov
  • 1,678
  • 1
  • 13
  • 22
  • It's really cool! I would never have thought of this on their own. Now I will write the code a little bit better quality. Thank You for your experience! – Joe Nov 15 '16 at 23:12
2

There is a wordpress function for that:

is_plugin_active('advanced-custom-fields/acf.php');

Note: You might run into issues when changing to the premium version of a plugins.

Tobi G.
  • 1,530
  • 2
  • 13
  • 16
0

Yes, it's a good way to check if the plugin function exists.

You can also try is_plugin_active function to check if the plugin is activated, because the function can be redeclared somewhere.

I think the main reason you are doing that is to prevent Fatal Errors, so it doesn't matter which way you can use.

Stanimir Stoyanov
  • 1,876
  • 1
  • 15
  • 20
  • ACF can be shipped as a library within a theme framework or another plugin. `function_exists()` is a safer way to check. – Ihor Vorotnov Nov 14 '16 at 20:06
  • Correct, but if it's a library within a theme framework or another plugin, it's expected that it will be activated by the theme or another plugin. – Stanimir Stoyanov Nov 14 '16 at 20:09
  • No, not necessary. If it's shipped as a library or composer package, it may be used as an actual library, loaded via simple include / autoloading. In this case, it won't be activated as a plugin or even listed in plugins at all. It will be loaded, but not as an active plugin, I use it this way with Composer all the time. – Ihor Vorotnov Mar 29 '19 at 11:30