PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Roles and capabilities - granting permissions to editors

  1. Hi, I see that Gravity Forms offers some core capabilities.

    http://www.gravityhelp.com/documentation/role-management/

    I'd like to tap into these without using the Members plugin, in part because I am using WPMU and Members does not affect roles globally in WPMU.

    Basically, I want to allow all EDITORS to do all Gravity Forms-related capabilities.

    Since all of my blogs are based off of a parent theme, can I simple add this to my theme's functions.php file?

    $role = get_role( 'editor' );
    $role->add_cap( 'gravityforms_create_form' );
    $role->add_cap( 'gravityforms_delete_entries' );
    $role->add_cap( 'gravityforms_delete_forms' );
    $role->add_cap( 'gravityforms_edit_entries' );
    $role->add_cap( 'gravityforms_edit_entry_notes' );
    $role->add_cap( 'gravityforms_edit_forms' );
    $role->add_cap( 'gravityforms_edit_settings' );
    $role->add_cap( 'gravityforms_export_entries' );
    $role->add_cap( 'gravityforms_feed' );
    $role->add_cap( 'gravityforms_uninstall' );
    $role->add_cap( 'gravityforms_view_entries' );
    $role->add_cap( 'gravityforms_view_entry_notes' );
    $role->add_cap( 'gravityforms_view_settings' );

    and this will add these capabilities to the Editor role for any site using that theme?

    I've tested this and it seems to work fine. However, I am just asking for a sanity check. Is this the right way to do this?

    Posted 14 years ago on Wednesday June 9, 2010 | Permalink
  2. A safer way might be to wrap that code in a is_plugin_active conditional. Like this:

    if (is_plugin_active('gravityforms/gravityforms.php')) {
            $role = get_role( 'editor' );
            $role->add_cap( 'gravityforms_create_form' );
            $role->add_cap( 'gravityforms_delete_entries' );
            $role->add_cap( 'gravityforms_delete_forms' );
            $role->add_cap( 'gravityforms_edit_entries' );
            $role->add_cap( 'gravityforms_edit_entry_notes' );
            $role->add_cap( 'gravityforms_edit_forms' );
            $role->add_cap( 'gravityforms_edit_settings' );
            $role->add_cap( 'gravityforms_export_entries' );
            $role->add_cap( 'gravityforms_feed' );
            $role->add_cap( 'gravityforms_uninstall' );
            $role->add_cap( 'gravityforms_view_entries' );
            $role->add_cap( 'gravityforms_view_entry_notes' );
            $role->add_cap( 'gravityforms_view_settings' );
    }

    BUT, this generates an error, saying that is_plugin_active is not defined. d'oh!

    Posted 14 years ago on Wednesday June 9, 2010 | Permalink
  3. Your method above will work. A couple of notes:

    To test if Gravity Forms is active, use:

    if(class_exists("RGForms")){
    ...
    }

    Also, it is probably not good form to call $role->add_cap('....') at every request. You may want to use the theme activation hook so that your code only runs when the theme gets activated, or you can implement your own logic with an option. Something like

    if( ! get_option('has_loaded_permissions', true) ){
       //permissions have not been set, set them now
      $role = get_role( 'editor' );
      $role->add_cap( 'gravityforms_create_form' );
      .......
      .......
    
      //update option so that we don't set the permissions again
      update_option('has_loaded_permissions', true);
    }

    I hope this helps.

    Posted 14 years ago on Wednesday June 9, 2010 | Permalink
  4. excellent, thanks.

    Posted 14 years ago on Wednesday June 9, 2010 | Permalink
  5. Hi Guys,

    Looks like a great solution,

    I'm still trying to get to grips with PHP, I want to add the same functions for editors on my WP site.

    @wheatoncollege could you past the code that you ended up using as mine is returning errors.

    Really appreciate it.

    Thanks

    Posted 14 years ago on Monday June 28, 2010 | Permalink
  6. @AspectWD If you want role management you don't have to write it yourself. Gravity Forms integrates with the Members plugin which adds complete role management to WordPress.

    You can download it here:

    http://wordpress.org/extend/plugins/members/

    Posted 14 years ago on Monday June 28, 2010 | Permalink

This topic has been resolved and has been closed to new replies.