Give View Entry access to a WordPress Role

, , , ,

So I came across this snippet of code recently when I was trying to find a way to give a user access to a form’s entries based on a user role.  I did find that I needed to modify it a little bit for the menu side of things so that when you go to the view entries page without having selected a menu, it will not show the results of that form.  If left unedited it would of showed the first form added to the systems entries.  This way it hides this menu item.  So you should however also add a custom menu item for that role to that forms entries which you can see the example code below.

/**
 * Restrict access to GravityForms to users with a certain capability. Usage: uny_access_[ID].
 *
 * @since 0.1.0
 * 
 * http://stackoverflow.com/questions/33619656/change-capability-management-to-admin-editable-user-setting
*/

function uny_granular_form_management( $all_caps, $cap, $args ) {
    $cap = reset( $cap );

    if ( strpos( $cap, 'gravityforms_' ) !== 0 )
            return $all_caps;

    // save initial values
    $view_entries = isset( $all_caps['gravityforms_view_entries'] ) ? $all_caps['gravityforms_view_entries'] : 0;
    $export_entries = isset( $all_caps['gravityforms_export_entries'] ) ? $all_caps['gravityforms_export_entries'] : 0;
    $edit_forms = isset( $all_caps['gravityforms_edit_forms'] ) ? $all_caps['gravityforms_edit_forms'] : 0;

    if (current_user_can('administrator')) {
	    $all_caps['gravityforms_view_entries'] = 1;
	    $all_caps['gravityforms_export_entries'] = 1;
	    $all_caps['gravityforms_edit_forms'] = 1;
    } else {
	    // add these so that the menus show up
	    $all_caps['gravityforms_view_entries'] = 0;
	    $all_caps['gravityforms_export_entries'] = 1;
	    $all_caps['gravityforms_edit_forms'] = 0;
	}
    // viewing entries, editing form
    $form_id = isset( $_GET['id'] ) ? $_GET['id'] : null;

    // exporting entries
    if ( ! $form_id )
            $form_id = isset( $_POST['export_form'] ) ? $_POST['export_form'] : null;

    // see if the capability exists for this form for this user
    $per_form_cap = 'uny_access_' . $form_id;

    if ( $form_id && isset( $all_caps[ $per_form_cap ] ) && 1 == $all_caps[ $per_form_cap ] ) {
            // enable all caps when viewing authorized form
            $all_caps['gravityforms_edit_entries'] = 1;
            $all_caps['gravityforms_edit_entry_notes'] = 1;
            $all_caps['gravityforms_edit_forms'] = 0;
            $all_caps['gravityforms_export_entries'] = 1;
            $all_caps['gravityforms_delete_entries'] = 1;
            $all_caps['gravityforms_preview_forms'] = 0;
            $all_caps['gravityforms_view_entries'] = 1;
            $all_caps['gravityforms_view_entry_notes'] = 1;
    } elseif ( $form_id ) {
            // return caps to defaults when on potentially unauthorized form
            $all_caps['gravityforms_view_entries'] = $view_entries;
            $all_caps['gravityforms_export_entries'] = $export_entries;
            $all_caps['gravityforms_edit_forms'] = $edit_forms;
    }

    return $all_caps;
}
add_filter( 'user_has_cap', 'uny_granular_form_management', 10, 3 );

add_action('admin_menu', 'add_custom_entries');

function add_custom_entries() {
		add_menu_page('View Form 1 Entries', 'Form 1 Entries', 'uny_access_1', 'admin.php?page=gf_entries&view=entries&id=1');
		add_menu_page('View Form 2 Entries', 'Form 2 Entries', 'uny_access_2', 'admin.php?page=gf_entries&view=entries&id=2');
}

 

Skills

, ,

Posted on

November 3, 2016

Submit a Comment

Your email address will not be published. Required fields are marked *