Wordpress - Removing an admin page added by a 3rd party plugin. Gravity forms in this example
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 );
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...
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.
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.