add_menu_page item show to Editor Role Wordpress - 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;
}

Related

capabilty in WordPress to show menu to all registered users?

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 );
?>

Create posts, pages and menus in Wordpress MU

I'm using Wordpress (with multisite capabilities enable) to create my network.
When I add a new site, it's needed to create some posts, pages and menus into this new site.
I need to create a default form (with WP Contact Form 7 plugin) too.
While I'm looking for this answer, I'm doing this manually: add the new site, create posts, pages, menus and default contact form, then I give the user and password to new user.
Thanks!!
It can be done with a Must Use Plugin and the action hook wpmu_new_blog.
<?php
/**
* Plugin Name: Create custom content on site creation
*/
add_action( 'wpmu_new_blog', 'custom_items_so_23276440', 10, 6 );
function custom_items_so_23276440( $blog_id, $user_id, $domain, $path, $site_id, $meta )
{
switch_to_blog( $blog_id );
$default_page = array(
'post_title' => 'Default title',
'post_content' => '<h2>Default content</h2>',
'post_status' => 'publish',
'post_type' => 'page'
);
// insert the post into the database
wp_insert_post( $default_page );
restore_current_blog();
}
To create the menus, use the functions wp_create_nav_menu and wp_update_nav_menu_item. I'm not sure about the contact form, you'll have to inspect the database to see how CF7 does it, maybe it has some handy function to create a form programatically...
There's another technique using the file wp-content/install.php explained in How to Create a Custom WordPress Install Package?

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.

add menu item to the dashboard menu of wordpress

I am trying the following syntax to add menu-item on dashboard:
<?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>
But, I am confused where to add this code.
Whether to add it in my theme's functions.php or my plugin's functions.php ?
Or any other way to add custom menu-item on the dashboard of the wordpress logged in as admin ?
I want to add custom menu item like the following image:
What you see in the screenshot is the screen of a Custom Post Type. In the documentation you will find an example code on how to add such a screen.
About where to place the code - it depends. Do you want to be able to use this Custom Post Type in other themes, or you will only need it in this theme?
If you want to use it in other themes as well, put the code in your plugin's code.
If you want to use it only in this theme, put it in your theme's functions.php.
And in case you still want to add a custom menu page, here you will find examples on what is the proper way to use this function. What you should note, is that the call to add_menu_page() should be done inside a function that is run on the admin_menu action.
Here's an example working code with WP 3.4.2
function register_custom_menu_page() {
add_menu_page('custom menu title', 'custom menu', 'add_users', 'custompage', '_custom_menu_page', null, 6);
}
add_action('admin_menu', 'register_custom_menu_page');
function _custom_menu_page(){
echo "Admin Page Test";
}
This is a perfect answer.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
)
);
}

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