Gravity Forms add-ons need to have capabilities

TIL that you need to give Gravity Forms add-ons some capabilities, otherwise they might disappear from the WordPress admin.

So I have a few Gravity Forms add-ons out there, some of them using the Gravity Forms add-on framework. The framework makes it really easy to build add-ons, in general. OK, so it makes a few bits harder, but mostly it’s easier with the framework.

One nice thing the framework does is automatically manage menu entries for you, like the Settings menu and Form Settings menu links for your add-on. At least, it did, until suddenly it didn’t.

It turns out that if someone has assigned specific permissions for roles in WordPress, that can get messed up. There’s a very handy plugin called Members that lets you do that. If you happen to assign, say, the gravityforms_view_settings permission to administrators, well… maybe your add-on will disappear from the Settings menu. Same with the Form Settings menu with some other permissions.

To avoid this situation, all you need to do is define the permissions that your add-on needs. You can do that in the constructor for your add-on class. I went with reusing the permissions that Gravity Forms already creates, like this:

public function __construct() {
    // ...other add-on settings...

    // define capabilities in case role/permissions have been customised (e.g. Members plugin)
    $this->_capabilities_settings_page  = 'gravityforms_edit_settings';
    $this->_capabilities_form_settings  = 'gravityforms_edit_forms';
    $this->_capabilities_uninstall      = 'gravityforms_uninstall';

    parent::__construct();

    // add-on hooks...
}

If you want, you can create some custom permissions and use them instead. That’s actually what most of Gravity Forms’ own add-ons do, for fine-grained control of who can do what. Like this:

public function __construct() {
    // ...other add-on settings...

    // define capabilities in case role/permissions have been customised (e.g. Members plugin)
    $this->_capabilities                = array('gravityforms_myaddon', 'gravityforms_myaddon_uninstall');
    $this->_capabilities_settings_page  = 'gravityforms_myaddon';
    $this->_capabilities_form_settings  = 'gravityforms_myaddon';
    $this->_capabilities_uninstall      = 'gravityforms_myaddon_uninstall';

    parent::__construct();

    // add-on hooks...
}

Job can be done even when someone moves the cheese. Oh, and Happy 2017!

Facebooktwittergoogle_plusredditlinkedinmailFacebooktwittergoogle_plusredditlinkedinmail