Show custom tabs on specific user role profiles in Youzer plugin? - wordpress

I made a custom tab in youzer profile and I need to show it to specific roles' profiles only.
Please let me know any hooks that I can use to accomplish this functionality.

Here's how to change the tabs using a filter. ($bpCoreNavItem is an instance of BP_Core_Nav_Item)
function filterProfileTabs(array $tabs) {
$newTabs = [];
foreach ($tabs as $bpCoreNavItem) {
if ($bpCoreNavItem->slug !== 'posts') {
$newTabs[] = $bpCoreNavItem;
}
}
return $newTabs;
}
add_filter('yz_profile_primary_nav', 'filterProfileTabs');
In that code I'm simply removing the "Posts" tab when finding it by its slug. To remove items based on user roles, you'll want to look up the user's role and implement any logic based on the given role. (Ex. Only remove "Posts" for users without the administrator role or the like.)

Related

Modify User Roles in WordPress?

I have been working on a client's WordPress Website and last day my client want to hide navigation menu and pages from author/contributor categories.
I have searched and tried some of the plugin but didn't get the exact thing. Please let me know what should i use to hide some pages from user and from navigation.
Only Admin can see all the pages and other members should see only 1 section that is allowed to visible for them.
Thank You
use this plugin to manage All roll:
http://wordpress.org/plugins/user-role-editor/
Here is the Complete function for removing each Menu and submenu from wp-admin for another user:
function remove_menus() {
global $menu, $submenu;
$restricted = array(__('Dashboard'), __('Profile'), __('Users'), __('Tools'), __('Comments'), __('Settings'), __('Plugins')); //Here you can also define the name like Pages
end($menu);
while (prev($menu)) {
$value = explode(' ', $menu[key($menu)][0]);
if (in_array($value[0] != NULL ? $value[0] : "", $restricted)) {
unset($menu[key($menu)]);
}
}
unset($menu[5]); // this is just for example
unset($submenu['edit.php'][16]); // this is just for example
}
Now You have to put a conditon for other user i.e:
$thisusername = $current_user->user_login; // this is to get the current user login
if (($thisusername == "user123")) {
add_action('admin_menu', 'remove_menus');
}
Note: You can find many plugins but all of them are not in depth like this code.Well you can try this plugin to manage your user's roles.Capability Manager Plugin

Wordpress, filter pages in Admin edit screen

Is it possible to 'filter' which pages are shown in the 'edit' screen for pages ( http://cl.ly/6nLC ) in Wordpress? I have looked in the action / hook section of Wordpress for plugin developers but I could not find any.
What I am trying to accomplish is is that certain users can edit certain pages (and child pages) and other persons cannot edit those pages but might be able to edit other pages.
I have allready written a plugin which makes it possible to put different users in differtent groups, which now just needs to have different rights, which user is member of which group is stored in the user_meta table.
However if there is 'any' filter hook / method for this, can someone point this out, I think I will be able to go further from there.
Kind regards.
You can use a posts_where filter to add a condition to the SQL query to filter out some pages. A load-{filename} action can be used to ensure the filter is only applied when managing pages.
add_action('load-edit.php', 'my_load_edit_php_action');
function my_load_edit_php_action() {
if ($_GET['post_type'] !== 'page') return;
add_filter('posts_where', 'my_posts_where_filter');
}
function my_posts_where_filter($sql) {
if (current_user_can('your_capability')) {
global $wpdb;
$sql = " AND $wpdb->posts.ID NOT IN (1,2,3)" . $sql;
}
return $sql;
}

remove "profile" admin-menu from administrative panel

I am using WordPress, and I want to remove "profile" menu-option completely
Any one is having idea how can I achieve this?
Thanks
For the sake of completeness, here's how to do it programmatically...
// Run the function on admin_init
add_action('admin_init', 'remove_profile_menu');
// Removal function
function remove_profile_menu() {
global $wp_roles;
// Remove the menu. Syntax is `remove_submenu_page($menu_slug, $submenu_slug)`
remove_submenu_page('users.php', 'profile.php');
/* Remove the capability altogether. Syntax is `remove_cap($role, $capability)`
* 'Read' is the only capability subscriber has by default, and allows access
* to the Dashboard and Profile page. You can also remove from a specific user
* like this:
* $user = new WP_User(null, $username);
* $user->remove_cap($capability);
*/
$wp_roles->remove_cap('subscriber', 'read');
}
I know this is late but I just stumbled on this and thought I would add to it. That does remove the sub-menu profile menu item but does not remove the menu profile item. For someone like me who has created a completely custom profile page, I don't want my users to access the profile.php page at all. So this code will work for that:
function remove_profile_menu() {
remove_submenu_page('users.php', 'profile.php');
remove_menu_page('profile.php');
}
add_action('admin_menu', 'remove_profile_menu');
And if you only want to do this for certain capabilities....use this code:
function remove_profile_menu() {
// Only the Admin can see the profile menu
if(!current_user_can('update_core')) {
remove_submenu_page('users.php', 'profile.php');
remove_menu_page('profile.php');
}
}
add_action('admin_menu', 'remove_profile_menu');
You can use the current_user_can() function to determine who you want to see the menu items.
Profiless plugin does that on the subscriber-level.
If you wish to do that for other groups, you should probably use it in combination with Capability manager plugin.

Drupal Views exposed filter of Author name as a drop down

This is a follow up question to Drupal Views exposed filter of Author name. The following question was answered and works. I can filter a view by user name. The user name is entered is entered by typing in a box and the box then auto completes. Rather then doing this I would like the list of users as a drop down. I only need one user to be selected. Do you know if this is possible?
You'll need a custom module for that.
I've done this for Drupal 7 this way: create a module, say, views_more_filters, so you have a views_more_filters.info file like this:
name = Views More Filters
description = Additional filters for Views.
core = 7.x
files[] = views_more_filters_handler_filter_author_select.inc
files[] = views_more_filters.views.inc
(file views_more_filters_handler_filter_author_select.inc will contain our filter handler).
A basic views_more_filters.module file:
<?php
/**
* Implements of hook_views_api().
*/
function views_more_filters_views_api() {
return array('api' => 3);
}
Then define your filter in views_more_filters.views.inc:
<?php
/**
* Implements of hook_views_data().
*/
function views_more_filters_views_data() {
return array(
'node' => array(
'author_select' => array(
'group' => t('Content'),
'title' => t('Author UID (select list)'),
'help' => t('Filter by author, choosing from dropdown list.'),
'filter' => array('handler' => 'views_more_filters_handler_filter_author_select'),
'real field' => 'uid',
)
)
);
}
Note that we set author_select as a machine name of the filter, defined filter handler ('handler' => 'views_more_filters_handler_filter_author_select') and a field we will filter by ('real field' => 'uid').
Now we need to implement our filter handler. As our filter functions just like default views_handler_filter_in_operator, we simply extend its class in views_more_filters_handler_filter_author_select.inc file:
<?php
/**
* My custom filter handler
*/
class views_more_filters_handler_filter_author_select extends views_handler_filter_in_operator {
/**
* Override parent get_value_options() function.
*
* #return
* Return the stored values in $this->value_options if someone expects it.
*/
function get_value_options() {
$users_list = entity_load('user');
foreach ($users_list as $user) {
$users[$user->uid] = $user->name;
}
// We don't need Guest user here, so remove it.
unset($users[0]);
// Sort by username.
natsort($users);
$this->value_options = $users;
return $users;
}
}
We haven't had to do much here: just populate options array with a list of our users, the rest is handled by parent class.
For further info see:
Views API
Where can I learn about how to create a custom exposed filter for Views 3 and D7? on Drupal Answers
Demystifying Views API - A developer's guide to integrating with Views
Sophisticated Views filters, part2 - writing custom filter handler (in Russian, link to Google translator)
Tutorial: Creating Custom Filters in Views
Yes, this is possible. Its not particularly tough to do this... but its slightly tedious. You need to create two views
The first view is a list of users on your system (a View of type Users). This user list is displayed as a dropdown instead of a list (using jump menu view style). Clicking on any user within this dropdown will call the second view with the uid (user id) of the selected user as the argument in the URL. This view is a block.
The second view is a simple Node listing. It is a page view at a particular URL. It takes 1 argument which is the uid (user id) of the user.
Detailed Steps
Download the Ctools module
http://drupal.org/project/ctools
Enable the Chaos Tools Module. This
module provides a Views Style Plugin
called "Jump Menu"
Create a new view of type Users and NOT type Node which you usually
create. In the fields add User:
Name and User: uid. For the
settings of User: uid, make sure
you click on Rewrite the output of
the field. The rewritten output of
the field should be
my_node_list/[uid]. Make sure you
select the exclude from display checkbox.
In the settings for Style in the view, select the Jump Menu style. Click on the settings for the style. Make sure the Path dropdown has User: uid choosen
Add a block display to the view. Name the block User Drop Down
Save the view
Add the block User Drop Down to any region in your theme e.g. Content Top (usually the best) or left sidebar. Make sure the block is only visible at the urls my_node_list/* and my_node_list by setting the block visibility settings
Now create another view of type Node. Add an argument field User: uid. Add the fields you are interested in e.g. Node: title, User: Name etc.
Add a page display. Let the page be at the url my_node_list
Save the view. Test the dropdown with its list of users on the system at http://yoursitename/my_node_list
http://drupal.org/project/better_exposed_filters
Check this one
I think you just have to choose "Taxonomy:term The taxonomy term ID" instead of "name".
Found a simple solution here. http://bryanbraun.com/2013/08/06/drupal-tutorials-exposed-filters-with-views

How to redirect user to a specific page after they login if they belong to a certain role?

We have certain users in our member list that have a role "vendor" attached to them. All such members are to be redirected to a certain page upon login. How can this be accomplished?
There is more than one way to skin this cat...
This is my preferred Drupal 7 method:
function hook_user_login(&$edit, $account) {
$edit['redirect'] = 'node/123';
}
For Drupal 7
Action --> admin/config/system/actions - Redirect to URL
then enable your trigger module
Trigger --> /admin/structure/trigger/node
if your are trying to login redirect just follow this(select user tab in the page)
go to --> admin/structure/trigger/user
then
Trigger: After a user has logged in
choose an action -->Redirect to URL and assign.
Then clear the cache.
It will work for you!
You can define actions and triggers in Drupal:
Action(admin/settings/actions)
- Redirect to a specific page
Trigger (admin/build/trigger/user)
- After user has logged in
Try this.
EDIT (see comments):
Create a small module to check on a user's login process what role he has and then redirect if neccesary.
drupal_goto => redirect-function in drupal
hook_user =>triggers on user operations
And for the user's roles:
GLOBAL $user;
$roles = $user->roles;
$vendor = in_array('vendor', $roles);
$vendor then holds a true/false value will decide to redirect or not.
If you don't know how to do this, just post here and I'll write the module for you. But this would be a good practice for writing future drupa modules for you perhaps. :)
There are 2 ways in DRUPAL 7
1) Using action and trigger
see this http://drupal.org/node/298506
2)if using custom module
function YOURMODULE_user_login(&$edit, $account) {
if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset' || variable_get('login_destination_immediate_redirect', FALSE)) {
if(in_array('THE-ROLE-WANTED-TO-REDIRECT',$account->roles)):
drupal_goto('PATH');
else: drupal_goto('user/'.$account->uid);
endif;
}
}
You can use Rules
Events: User has logged in.
Condition: User has role
Actions: Page redirect
There are modules that do this (besides Trigger+Actions), such as LoginDestination: http://drupal.org/project/login_destination. This Drupal forum post has a bit more info about it as well.
following condition for hook_user
if($op =='login') drupal_goto("your path");
This can be achieved by using a combination of content access and login toboggan modules. You'll be able to restrict pages and prompt user to login to access them.
First set the conditions in form preprocess (for example I want to redirect only users that logged in using the form at node page)
function YOURMODULE_form_user_login_alter(&$form, &$form_state, $form_id)
{
$pathArguments = explode('/', current_path());
if (count($pathArguments) == 2 && $pathArguments[0] === 'node' && is_numeric($pathArguments[1])) {
$form_state['nodepath'] = current_path();
}
}
than define redirect:
function YOURMODULE_user_login(&$edit, $account)
{
if (isset($edit['nodepath']) && !empty($edit['nodepath'])) {
drupal_goto($edit['nodepath']);
}
}

Resources