WP Backend: Create custom menu for specific User Group - wordpress

I am using Members plugin to manage User Groups (restrict pages/posts for specific user group) now to show the links to that user group in back-end I need a plugin for creating Custom Menu where the allowed pages/posts links will be shown (back-end) for the specific user group.

Sorry, dont know exatly which plugin or code would solve your problem.
But here are some resources:
http://codex.wordpress.org/Roles_and_Capabilities#Resources
http://wordpress.org/extend/plugins/tags/capability

Well what I do is to add this script in my theme's function.php
// Admin bar menu w.r.t role
function wp_admin_bar_new_item() {
global $wp_admin_bar;
if (current_user_can('contractor')){
$wp_admin_bar->add_menu(array(
'id' => 'wp-admin-bar-new-item',
'title' => __('Contractor Menu'),
'href' => 'http://my_site/contractor/'
));
} elseif (current_user_can('consultant')) {
$wp_admin_bar->add_menu(array(
'id' => 'wp-admin-bar-new-item',
'title' => __('Consultant Menu'),
'href' => 'http://my_site/consultant/'
));
}
}
add_action('wp_before_admin_bar_render', 'wp_admin_bar_new_item');
and fix my issue.

Related

Rename plugin name in Wordpress Dashboard Menu

I have tried a lot of suggestions on different posts here, but none of them seem to work for me.
I want to rename the name of a plugin on the dashboard menu on my Wordpress site. The name of the plugin is Sensei LMS and the path of the plugin folder is /plugins/sensei-lms.
I'd appreciate if someone helps me with some code that I can use to rename this. Thank you
I've used this tutorial in the past.
In your case, it would be:
function my_renamed_admin_menu_items() {
global $menu;
// Define your changes here
$updates = array(
"Sensei LMS" => array(
'name' => 'New Menu Name'
),
"Another Menu Name" => array(
'name' => 'Another New Name'
)
);
foreach ($menu as $k => $props) {
// Check for new values
$new_values = (isset($updates[$props[0]])) ? $updates[$props[0]] : false;
if (!$new_values) continue;
// Change menu name
$menu[$k][0] = $new_values['name'];
}
}
add_action('admin_init', 'my_renamed_admin_menu_items');

Layered and Role Based Taxonomy in Wordpress

I want to achieve something like this:
admin can create taxonomy. (already have)
admin assign a taxonomy to specific vendor. (already have)
vendor can create more taxonomy, with an extra field keeping track that the taxonomy belongs to that specific vendor.
Now i'm in the mist giving permission to vendor to edit taxonomy, and the logic behind. I have the following code in my includes/taxonomy.php in my plugin:
function WCPVPlus_register_taxonomy_args( $args, $taxonomy, $object_type ) {
if(MY_TAXONOMY===$taxonomy){
$args['capabilities'] =array(
'manage_terms' => 'read',
'edit_terms' => 'read',
'delete_terms' => 'read',
'assign_terms' => 'read',
);
};
return $args;
}
add_filter('register_taxonomy_args', 'WCPVPlus_register_taxonomy_args', 10, 3);
then in my main php file, i have this in the init method:
include_once('includes/taxonomy.php');
The code doesn't seem to work, as when i debug using xdebug, the code doesn't stop at WCPVPlus_register_taxonomy_args function.
Is there anyone here to explain the concept behind and enlighten me in achieving what i'm trying to do?

Creating a delete confirmation for images using the Wordpress meta box plugin

I am using the Meta Box plugin for Wordpress. I can successfully create fields in the cms for users to upload images. I would like to extend this in two ways:
First, I would like a delete confirmation when users remove an image from the image gallery
Here is the code:
$meta_boxes[] = array(
'id' => 'project_media',
'title' => 'Project Media',
'pages' => array( 'project' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Media Gallery',
'desc' => 'Images should be sized to 983px x 661px',
'id' => $prefix . 'project_media_gallery',
'type' => 'image'
)
);
This creates upload functionality in the custom post type where users can add images to a slideshow. The problem is if the user accidentally clicks the delete button, there is no confirmation to make sure it is deleted. Can I somehow extend the plugin through functions and call an alert when this button is clicked? Something that does not involve editing the WP core?
Second, the base functionality requires the user to upload an image from their local machine. Is there a way to tap into the Media Library for this?
No idea how to even start tackling this one.
To answer the first question
First, I would like a delete confirmation when users remove an image from the image gallery
You can do that by calling a custom script file from the functions.php.
function alert_delete() {
if(is_admin()){
wp_register_script( 'alert_delete', get_bloginfo('template_url'). '/js/alert_delete.js', array('jquery'));
wp_enqueue_script('alert_delete');
}
}
and create a file named alert_delete.js in the js directory of your theme.
alert_delete.js:
// admin delete check
jQuery(document).ready(function(){
jQuery(".rwmb-delete-file").click(function() {
if (!confirm("Are you sure? This process cannot be undone.")){
return false;
}
});
});
In response to the second question...
Second, the base functionality requires the user to upload an image
from their local machine. Is there a way to tap into the Media Library
for this?
Get the latest version of the Meta Box Plugin first.
then change
'type' => 'image'
to
'type' => 'image_advanced'
which will allow you to upload from the existing Media Gallery or a new file from your computer.

Drupal programmatically adding an item to a menu

I wish to conditionally add an item to a menu. I have a custom module and a menu called "links". How would I add an item to a menu in my module code?
You need to implement hook_menu in your module. Example:
<?php
function mymodule_menu() {
$items['mymodule/links'] = array(
'title' => 'Links',
'page callback' => 'mymodule_links_page',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
?>
The 'type' => MENU_SUGGESTED_ITEM, part makes it optional, so it can be enabled by the end user - is that what you meant with "conditionally"? If not, please explain what kind of "conditionally" you're looking for.
Or you can use the 'type' => MENU_NORMAL_ITEM, since it is enabled by default, but can be disabled any time. This of course depends on your preferences. See http://api.drupal.org/api/drupal/includes--menu.inc/group/menu/7 for further reference.
Another good thing to know when using module defined menu items in custom menus might be how to programmatically create the menu you want to use so that everything is created "out of the box". Simply add a mymodule.install-file where you put the following code:
<?php
function mymodule_install() {
$menu = array(
'menu_name' => 'links',
'title' => 'My Custom Links',
'description' => 'Descriptive text.',
);
menu_save($menu);
}
?>
If you have an uninstall function, don't forget to not only deactivate the module, but also to uninstall it. Reenable the module, flush your caches and the menu item should be there!
You can dynamically show or hide a menu item based on a condition ( access callback ).
Here is an example from https://drupal.org/project/examples:
<?php
function mymodule_menu() {
$items = array();
$items['my-menu-item'] = array(
'title' => 'My Menu',
'description' => 'My description',
'page callback' => 'my_page_link_callback_function_name',
'access callback' => 'can_the_user_see_this_item',
'expanded' => TRUE,
'weight' => -100,
'menu_name' => 'primary-links',
);
return $items;
}
// Here we determine if the user can or can not see the item.
function can_the_user_see_this_item(){
if (MY_CONDITION){
return TRUE;
}
else {
return FALSE;
}
}
The menu system is cached, so you can't add or remove menu items as you please based on user, page viewed, custom logic etc. That is you can't do it without having to clear the menu cache which would cause a severe performance hit.
What you could do, to create this effect is to create some custom logic to define the access control on the menu item. Since Drupal hides menu items that users doesn't have access to, you could under certain circumstances deny permission to hide the menu item. This is a bit hackish solution.
Another solution which I would prefer, would be to use js or css to hide or show the menu. You could dynamically add/remove a class on the body to determine if the menu item should be shown or not. This would quickly become unmanagable if you need several of these kind of menu items, however.
Use menu_link_save() function
Saves a menu link.
After calling this function, rebuild the menu cache using menu_cache_clear_all().
Parameters
$item: An associative array representing a menu link item, with elements:
link_path: (required) The path of the menu item, which should be normalized first by calling drupal_get_normal_path() on it.
link_title: (required) Title to appear in menu for the link.
menu_name: (optional) The machine name of the menu for the link. Defaults to 'navigation'.
weight: (optional) Integer to determine position in menu. Default is 0.
expanded: (optional) Boolean that determines if the item is expanded.
options: (optional) An array of options, see l() for more.
mlid: (optional) Menu link identifier, the primary integer key for each menu link. Can be set to an existing value, or to 0 or NULL to insert a new link.
plid: (optional) The mlid of the parent.
router_path: (optional) The path of the relevant router item.
$existing_item: Optional, the current record from the {menu_links} table as an array.
$parent_candidates: Optional array of menu links keyed by mlid. Used by _menu_navigation_links_rebuild() only.
Return value
The mlid of the saved menu link, or FALSE if the menu link could not be saved.

WordPress: Disable "Add New" on Custom Post Type

Is there any way to disable the option of adding a new post under a Custom Post Type in WordPress (3.0)? I've looked into labels and arguments but can't find anything that would resemble such a feature.
There is a meta capability create_posts that is documented here and is used by WordPress to check before inserting the various 'Add New' buttons and links. In your custom post type declaration, add capabilities (not to be confused with cap) and then set it to false as below.
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => false, // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
),
'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
You'll probably want to set map_meta_cap to true as well. Without it, you won't be able to access the posts' editing pages anymore.
The combinations of the solutions above work in hiding the links (although someone could quite easily type the URL in directly.
The solution mentioned #3pepe3 relies on get_post_type() which will only work if there is already a post in the listing. If there are no posts, the function will not return anything, and the "Add New" link will be available. An alternative method:
function disable_new_posts() {
// Hide sidebar link
global $submenu;
unset($submenu['edit.php?post_type=CUSTOM_POST_TYPE'][10]);
// Hide link on listing page
if (isset($_GET['post_type']) && $_GET['post_type'] == 'CUSTOM_POST_TYPE') {
echo '<style type="text/css">
#favorite-actions, .add-new-h2, .tablenav { display:none; }
</style>';
}
}
add_action('admin_menu', 'disable_new_posts');
EDIT: To prevent direct access if someone types the URL in themselves: https://wordpress.stackexchange.com/a/58292/6003
WordPress Networks: I found that Seamus Leahy's answer doesn't work if you are logged in as a super admin of the network, it doesn't matter if the user doesn't have the capability, mapped or otherwise, when current_user_can($cap) is called by the CMS. By digging into the core I found you can do the following.
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => 'do_not_allow', // Removes support for the "Add New" function, including Super Admin's
),
'map_meta_cap' => true, // Set to false, if users are not allowed to edit/delete existing posts
));
The accepted answer hides the menu item, but the page is still accessible.
In wordpress and for all the post types there is the capability create_posts. This capability is used in several core files :
wp-admin\edit-form-advanced.php
wp-admin\edit.php
wp-admin\includes\post.php
wp-admin\menu.php
wp-admin\post-new.php
wp-admin\press-this.php
wp-includes\admin-bar.php
wp-includes\class-wp-xmlrpc-server.php
wp-includes\post.php
So if you really want to disable this feautere you must do it per role and per post type.
I use the great plugin "User Role Editor" to manage the capabilities per role.
But what about the capability create_posts? Well this capability is not mapped and also create_posts is equal to create_posts so we should fix this and map the capability per post type.
So you can add this piece of code in your functions.php and the you can manage this capability.
function fix_capability_create(){
$post_types = get_post_types( array(),'objects' );
foreach ( $post_types as $post_type ) {
$cap = "create_".$post_type->name;
$post_type->cap->create_posts = $cap;
map_meta_cap( $cap, 1);
}
}
add_action( 'init', 'fix_capability_create',100);
So here we are not hiding or removing menu elements... here we are removing the capability for users (including xmlrpc requests).
The action was init and not admin_init or anything else because init at priority 100 prevents the display of "add new" on admin bar, sidebar, etc (in all the wp interface).
Disable creating new post for registered post-types: (example for post and page)
function disable_create_newpost() {
global $wp_post_types;
$wp_post_types['post']->cap->create_posts = 'do_not_allow';
//$wp_post_types['page']->cap->create_posts = 'do_not_allow';
//$wp_post_types['my-post-type']->cap->create_posts = 'do_not_allow';
}
add_action('init','disable_create_newpost');
add_action("load-post-new.php", 'block_post');
function block_post()
{
if($_GET["post_type"] == "custom_type")
wp_redirect("edit.php?post_type=custom_type");
}
# Staffan Estberg,
This is best way to hide the Add New or Create New button in custom postypes
'capability_type' => 'post',
'capabilities' => array( 'create_posts' => false ),
'map_meta_cap' => true,
It disable to create new post in custom post types both side in admin menu and above the list of post type.
I found this simplest way for this. Just ad this code into theme’s function.php.
function hd_add_buttons() {
global $pagenow;
if (is_admin()) {
if ($_GET['post_type'] == 'custom_post_type_name') {
echo '<style>.add-new-h2{display: none !important;}</style>';
}
}
}
add_action('admin_head', 'hd_add_buttons');
As the question is 'how to disable add-new button on custom post type', and not 'how to restrict user editing custom post types', in my opinion the answer should be purely hiding the buttons with css, by adding this to the functions.php file :
add_action( 'admin_head', function(){
ob_start(); ?>
<style>
#wp-admin-bar-new-content{
display: none;
}
a.page-title-action{
display: none !important;
}
#menu-posts-MY-CUSTOM-POST-TYPE > ul > li:nth-child(3) > a{
display:none;
}
</style>
<?php ob_end_flush();
});

Resources