4

I'm trying to hide a help page from editors, added by the gravity forms plugin. I believe I've run into the same issue before with other plugins, so I'd like to know the correct way to do it for 3rd party plugins. This is what I have so far which does not work. It does of course work for other WP menus so I'm wondering if the function is only limited to WP menus?

function remove_menu_links() {
  if( !current_user_can('manage_options') ) {
    remove_menu_page('admin.php?page=gf_help'); // this is the pages url
  }
}
add_action( 'admin_menu', 'remove_menu_links');

6 Answers 6

3

Ok, Eugene's Answer works in the case of a plugin that doesn't deals with custom capabilities.

http://codex.wordpress.org/Roles_and_Capabilities
The WordPress Plugin API allows Roles and Capabilities to be added, removed and changed. Since Plugins might change Roles and Capabilities, just the default ones are addressed in this article.


So, if his code works without checking for the capability, we have to look how GravityForms executes his add_submenu_page action.
And for that, we drop the whole plugin folder inside a good code editor (NotePad++, TextMate, etc) and do a global search and find our stuff.

// wp-content/plugins/gravityforms/gravityforms.php
// all parameters removed from the original code, except $page_title and $capability
add_submenu_page( 
    $parent_slug, 
    __("Help", "gravityforms"),
    $menu_title,
    $has_full_access ? "gform_full_access" : $min_cap, 
    $menu_slug, 
    $function 
);

And a few lines before we see:

$has_full_access = current_user_can("gform_full_access");
$min_cap = GFCommon::current_user_can_which(GFCommon::all_caps());
if(empty($min_cap))
    $min_cap = "gform_full_access";

Now we go ahead with Members plugin, which btw GF recognizes, and we have the following in its config screen for the Editor role.
BUT NOTING THAT gform_full_access doesn't appears in this list. It has to be manually added through the plugin interface...

enter image description here

After that and marking the full access capability, the remove_submenu_page works as expected to the Editor role.



Reference code for all submenus (remembering the first one is the very top menu).

function remove_menu_links() {
    if( !current_user_can( 'manage_options' ) ) {
        // remove_submenu_page( 'gf_edit_forms', 'gf_edit_forms' ); 
        // remove_submenu_page( 'gf_edit_forms', 'gf_new_form' ); 
        // remove_submenu_page( 'gf_edit_forms', 'gf_new_formf_help' ); 
        // remove_submenu_page( 'gf_edit_forms', 'gf_entries' ); 
        // remove_submenu_page( 'gf_edit_forms', 'gf_settings' ); 
        // remove_submenu_page( 'gf_edit_forms', 'gf_export' ); 
        // remove_submenu_page( 'gf_edit_forms', 'gf_update' ); 
        // remove_submenu_page( 'gf_edit_forms', 'gf_addons' ); 
        remove_submenu_page( 'gf_edit_forms', 'gf_help' ); 
    }
}
add_action( 'admin_menu', 'remove_menu_links', 9999 );

Plugin of interest

Adminimize does this hiding magic in the blink of an eye and is completely PRO.

2
  • thank you for your comprehensive answer and for looking into the source of GF. I've now added the gform_full_access capability and the help has gone as expected. It does open up another can of worms in that all the other menu links are now accessible but I've hidden them all with the function :) I've had this problem in the past with disabling 3rd party menu links but I think I'm a lot more comfortable with it now.
    – Andrew
    Jul 18, 2012 at 3:04
  • @Andrew : I missed a couple of issues and updated my answer. Looks like the help page is considered pretty harmless and available to all caps current_user_can_which(GFCommon::all_caps()).. .. .. If you really want to block the access and not only hide the menu, probably some admin_head or admin_init coupled with current_user_can, $pagenow and wp_die could do the job.
    – brasofilo
    Jul 18, 2012 at 3:13
8

You need to add your hook at the end of the queue and then remove menu by slug:

function remove_menu_links() {
    if( !current_user_can( 'manage_options' ) ) {
        remove_menu_page( 'gf_edit_forms' ); // this is the pages url
    }
}
add_action( 'admin_menu', 'remove_menu_links', 9999 );

If you want to remove submenu you need to use following snippet:

function remove_menu_links() {
    if( !current_user_can( 'manage_options' ) ) {
        remove_submenu_page( 'gf_edit_forms', 'gf_help' ); 
    }
}
add_action( 'admin_menu', 'remove_menu_links', 9999 );
12
  • Thanks, didn't seem to work though. Any other ideas?
    – Andrew
    Apr 7, 2012 at 7:47
  • 1
    Probably you want to remove submenu item. I have updated my answer and added snippet to remove submenu item. Apr 7, 2012 at 7:54
  • I was unsuccessful with that also. The problem seems to be that it doesn't have a top menu slug. The full url when I hover over the link is website.com/wp-admin/admin.php?page=gf_help
    – Andrew
    Apr 7, 2012 at 8:01
  • @Andrew : Just edited the code and the remove_* works ok. The question is: do editors have GravityForms capability? . . . 'Cause that doesn't seem to be true.
    – brasofilo
    Jul 16, 2012 at 12:30
  • 1
    @brasofilo point taken ... i didn't mean anything negative by my comment. I've paid for a developers licence for Gravity Forms and always found that they've got back to me in < 24 hours. i think there is a page on GF dedicated to the hooks and roles & permissions.
    – Damien
    Jul 18, 2012 at 8:02
2

The GravityForms plugin renames the top level menu item to match the first of the sub-menu.

For example; if you've added just the gravityforms_view_entries capability to the Editor role, then the first sub-menu item will be "Entries", so the parent menu will be "gf_entries" not "gf_edit_forms". So, the following code will remove the "Help" item from the sub-menu for Editors with just that capability:

function remove_menu_links() {
    remove_submenu_page( 'gf_entries', 'gf_help' );
}
add_action( 'admin_menu', 'remove_menu_links', 9999 )
;

Hope this helps.

1

This was driving me insane. The answers provided here got me most of the way, but that particular darned Gravity Forms menu item is a little weird.

I managed to get to the root of it by analysing the $submenu global variable which contains all the menu data. The following snippet should kill the update submenu item for you, when hooked into admin_menu() as listed above :)

remove_submenu_page( 'gf_edit_forms', 'gf_update' ); 
0
0

Nothing above worked for me, but at least I can hide it with CSS.

#toplevel_page_gf_help{display:none;}

0

you can hide it with css only if you want, search for a plugin to add css on admin panel :

a[href="admin.php?page=gf_help"] {
    display: none !important;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.