capabilty in WordPress to show menu to all registered users? - wordpress

I am creating a plugin and adding menu in dashboard WordPress
add_submenu_page('documents', 'Add Document', 'Add Document', 'manage_options','add_document', 'my_plugin_options3');
I also want to show that menu option to my registered users who are subscribers.but when I do it for subscribers menu only appear in subscriber dashboard and when I use manage_options it only appears for admin, not subscribers. I want to show it on both.
add_menu_page( 'My Plugin Options', 'docs Management', 'subscriber', 'documents', 'my_plugin_options','',4 );
I tried Summary of Roles reference from here

Subscribers and Admins are roles, you should try using a capability within those roles, like you did for the first sub menu page you added. Subscribers have the "read" capability, as does every other user role, so I would start there.
add_menu_page( 'My Plugin Options', 'docs Management', 'read', 'documents', 'my_plugin_options','',4 );
If you only want to show the menu for Subscribers and Admins, and no other role, you could create your own capability, then assign it to your menu, like so:
<?php
global $wp_roles;
$wp_roles->add_cap( 'administrator', 'view_custom_menu' );
$wp_roles->add_cap( 'subscriber', 'view_custom_menu' );
add_menu_page( 'My Plugin Options', 'docs Management', 'view_custom_menu', 'documents', 'my_plugin_options','',4 );
?>

Related

add_menu_page item show to Editor Role Wordpress

I've been trying to add a menu to my WP Dashboard which I already accomplished. But now I want to show this menu to the Editor role as well as the administrator.
Here is my code
add_action( 'admin_menu', 'register_my_custom_menu_page' );
function register_my_custom_menu_page() {
add_menu_page( 'Instagram test', 'Instagram test', 'read', 'admin.php?page=sb-instagram-feed', '', 'dashicons-welcome-widgets-menus', 90 );
}
So far it works, but only for my administrator user, not to my Editors users.
I've read about the capabilities and that is why I put the read value on the function above.
How can I also show this menu to my Editors users?
Here is a screenshot, my custom menu is Instagram test.
Admin's Dashboard
Editor's Dashboard
While I can't attest to why read isn't working - generally if you want to limit something by user roles, you can put in the slug for the role. If you read the source for add_menu_page() it will actually run the capability through current_user_can which accepts a role slug as well.
I would replace read with editor and see what that gets you. It will also work for admins since it propagates down the list and administrators all have the editor, contributor, etc. "capabilities".
Edit: It appears you have the Instagram Feed plugin installed which will be conflicting with your custom plugin. The code from that plugin shows the sb-instagram-feed page belongs to that plugin:
function sb_instagram_menu() {
add_menu_page(
__( 'Instagram Feed', 'instagram-feed' ),
__( 'Instagram Feed', 'instagram-feed' ),
'manage_options',
'sb-instagram-feed',
'sb_instagram_settings_page'
);
add_submenu_page(
'sb-instagram-feed',
__( 'Settings', 'instagram-feed' ),
__( 'Settings', 'instagram-feed' ),
'manage_options',
'sb-instagram-feed',
'sb_instagram_settings_page'
);
}
add_action('admin_menu', 'sb_instagram_menu');
And that plugin requires manage_options, an administrator only capability. You'll need to not link to the page that other plugin makes, or deactivate that plugin.
Edit 2: Note that editing plugin files directly usually isn't a great practice as any changes you make will be overwritten when the plugin is updated. You might be able to unhook the current admin menu for it and hook in your custom one.
// Remove Existing Menu
remove_action( 'admin_menu', 'sb_instagram_menu' );
// Add Custom Menu
add_action( 'admin_menu', 'custom_sb_instagram_menu');
function custom_sb_instagram_menu() {
add_menu_page(
'Instagram Test',
'Instagram Test',
'editor',
'sb-instagram-feed',
'sb_instagram_settings_page'
);
add_submenu_page(
'sb-instagram-feed',
'Test Settings',
'Test Settings',
'editor',
'sb-instagram-feed',
'sb_instagram_settings_page'
);
}
This is the correct one for Admin and Editor role both.
add_menu_page( 'Transcoding Mp3', 'Transcoding Mp3', 'edit_pages', 'transcoding_mp3', 'transcoding_mp3_fun', '', 90 );
function transcoding_mp3_fun() {
$currentusr = wp_get_current_user();
$idcur = $currentusr->data->ID;
$namecur = $currentusr->data->user_login;
echo 'This is editor id = '. $namecur;
}

Woocommerce product tabs (show other products)

I am using woo commerce in my wordpress store online, I got a requirement that when a product is clicked and we navigate to the product detail page. I need to show the tabs into the product page like in the link
http://www.designbyhumans.com/shop/t-shirt/men/wolf-i/17011/
In this link two additional tabs are shown
1. Phone Cases
2. Art prints
Is there any way to achieve this is woo commerce?
Please help.
You can add custom tabs using woocommerce filters
The below code adds an extra tab called 'Product Description'.
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['desc_tab'] = array(
'title' => __( 'Product Description', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}
function woo_new_product_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo '<p>'.get_post_meta($prod_id,'product_description',true).'</p>';
}
You would need to fetch the product data into these tabs. In the above example data from a custom field(product_description) is fetched into the tab.

Redirect to submenu and hide menu link in submenus

So I have this code:
add_action('admin_menu', function() {
add_menu_page(
'Theme options',
'Theme options',
'manage_options',
'ef-theme-options',
'ef_theme_options_display',
'dashicons-admin-site'
);
add_submenu_page(
'ef-theme-options',
'E-shop options',
'E-shop options',
'manage_options',
'ef-eshop-options',
'ef_eshop_options_display'
);
});
This results in:
Theme options
- Theme options
- E-shop options
Is there easy way to redirect to first submenu if menu is clicked, and hide repeating submenu? Is there wordpress functionality that allows for this kind of manipulations?
Figured out myself. Adding remove_submenu after adding submenus functions removes first submenu and wp makes redirect to first real sumenu:
remove_submenu_page('ef-theme-options', 'ef-theme-options');
http://codex.wordpress.org/Function_Reference/remove_submenu_page
add_submenu_page(
null,//give that parent slug to null then it will hide from sidebar
'E-shop options',
'E-shop options',
'manage_options',
'ef-eshop-options',
'ef_eshop_options_display'
);

how do i add the post list to a newly admin panel in wordpress?

I have created a new sub menu for a custom post type in wordpress admin, and I would like to add the a modified list of posts in there, i accordance to the newly variables that i have in my database; is there a function in wordpress to display the post lists? or do i have to do it manually?
add_action('admin_menu', 'my_plugin_menu');
function my_plugin_menu() {
add_submenu_page( 'edit.php?post_type=product', __( 'My product list', 'zeeneo-tenders' ), __( 'My product list', 'woo-tenders' ), 'manage_options', 'my-products', 'my_plugin_options');
}
function my_plugin_options() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
echo "<h2>" . __( 'My products list', 'zeeneo-tenders' ) . "</h2>";
// Here is the code to display the list of posts in this new panel
}
WordPress doesn't have wp_dropdown_posts just like it has wp_dropdown_categories and wp_dropdown_users.
It has to be made by hand with get_posts or WP_Query, loop the results and make the <select><option>. For your use case, I think you'll need to use the Custom Fields parameters.
You can find a sample code at WPSE: Dropdown list of a custom post type.

How can i use use add_plugins_page() and order it on top of the installed plugins sub-menu

I am using the function add_menu_page
<?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>
and i need to put this in order.
I am making a more in depth plugin management page and i was hoping that i could list it under the plugin menu but above the other sub-menu options.
the current sub-menu order is
1.installed plugins
2.add new
3.editor
I want my plugin to be number 1 and shift all other down one level.
Any help is much appreciated! :)
Just like you have add_menu_page, there is also add_submenu_page which you can use.
Codex: http://codex.wordpress.org/Function_Reference/add_submenu_page
Just add the position by number to the end of your file.
add_menu_page( 'page title', 'menu title', 'manage_options', 'link-to-admin', '', plugins_url('/images/icon.png', __FILE__) 4);
In the case above, the menu title will be in the 4th position.
I know you asked this a long time ago, but there were no proper answers. Hope this helps you or others - PBM
function adjust_plugin_menu() {
// remove the existing plugin menu items
remove_submenu_page( 'plugins.php', 'plugins.php' ); // Plugins - Installed Plugins
remove_submenu_page( 'plugins.php', 'plugin-install.php' ); // Plugins - Add New Plugins
remove_submenu_page( 'plugins.php', 'plugin-editor.php' ); // Plugins - Editor
// add in your custom menu item
$destination = "path/to/your/plugin/code" ;
add_plugins_page (
'My Plugin Stuff',
'My Plugin Stuff',
'manage_options',
$destination );
// Put the other menu items back in
add_plugins_page('Installed Plugins', 'Installed Plugins', 'manage_options', 'plugins.php');
add_plugins_page('Add New', 'Add New', 'manage_options', 'plugin-install.php');
add_plugins_page('Editor', 'Editor', 'manage_options', 'plugin-editor.php');
}
add_action('admin_menu', 'adjust_plugin_menu');

Resources