Add Dashboard menu item and capability based on userID

I have a Contributor (with limited access in the Dashboard menu) and I want to give him access to the Contact Form DB plugin, so he can review form submissions. His userID=26.

Here is what I have so far:

// Add Dashboard menus based on userID
/* add our function to the admin menu action */
add_action( 'admin_menu', 'pxjn_add_menus', 999 );

function pxjn_add_menus() {

/* get the current user id */
$pxjn_current_user_id = $current_user->ID; // get the user ID

/* setup an array of user ids to be affected */
$pxjn_team_ids = array( 26 ); /*Ken Baker*/

/* if current user id is in the array */
if( in_array( $pxjn_current_user_id, $pxjn_team_ids ) ) {

/* add the menu items(s) */
add_menu_page ( 'admin.php?page=CF7DBPluginSubmissions' );
}

}

I think I am missing the part where I add a capability. How can I enhance this code to allow the Contributor to view what is normally an Admin plugin?

Thanks,
Larry

  • Vaughan

    Hi Larry,

    Hope you're well?

    It will all depend on the plugin and what capabilities it uses. in many cases, it'll probably require manage_options capability which is pretty much admin level.

    You can edit capabilities of a user by using the user role editor plugin.

    I've been looking at the code and it is hardcoded to require manage_options capability, so users will have to be admins to be able to access it.

    You can perhaps edit the plugin code tho.

    Open up & edit /wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php

    Find the following on or around lines 114 - 116

    if (!current_user_can('manage_options')) {
                wp_die(__('You do not have sufficient permissions to access this page.', 'community-yard-sale'));
            }

    Replace with;

    if (!current_user_can('contributor')) {
                wp_die(__('You do not have sufficient permissions to access this page.', 'community-yard-sale'));
            }

    Find the following on lines 1092 - 1097

    add_submenu_page($menuSlug,
                             $displayName . ' Options',
                             __('Options', 'contact-form-7-to-database-extension'),
                             'manage_options',
                             $settingsSlug,
                             array(&$this, 'settingsPage'));

    Replace with:

    add_submenu_page($menuSlug,
                             $displayName . ' Options',
                             __('Options', 'contact-form-7-to-database-extension'),
                             'contributor',
                             $settingsSlug,
                             array(&$this, 'settingsPage'));

    Open up & edit /wp-content/plugins/contact-form-7-to-database-extension/CF7DBOptionsManager.php

    Find on lines 285 - 287:

    if (!current_user_can('manage_options')) {
                wp_die(__('You do not have sufficient permissions to access this page.', 'contact-form-7-to-database-extension'));
            }

    Replace with:

    if (!current_user_can('contributor')) {
                wp_die(__('You do not have sufficient permissions to access this page.', 'contact-form-7-to-database-extension'));
            }

    Open up & edit /wp-content/plugins/contact-form-7-to-database-extension/CF7DBPluginLifeCycle.php

    Find around lines 110 - 15

    add_submenu_page('plugins.php',
                             $displayName,
                             $displayName,
                             'manage_options',
                             get_class($this) . 'Settings',
                             array(&$this, 'settingsPage'));

    Replace with:

    add_submenu_page('plugins.php',
                             $displayName,
                             $displayName,
                             'contributor',
                             get_class($this) . 'Settings',
                             array(&$this, 'settingsPage'));

    Find around lines 122 - 126

    add_options_page($displayName,
                             $displayName,
                             'manage_options',
                             get_class($this) . 'Settings',
                             array(&$this, 'settingsPage'));
        }

    Replace with:

    add_options_page($displayName,
                             $displayName,
                             'contributor',
                             get_class($this) . 'Settings',
                             array(&$this, 'settingsPage'));
        }

    Hopefully contributor roles will now be able to access the settings pages.

    Hope this helps

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.