Enable group creation for specific user role in BuddyPress - php

BuddyPress provides the "bp_user_can_create_groups" filter to restrict a user's ability to create groups. Reference - http://wordpress.org/support/topic/restrictallow-group-creation-by-user-role?replies=3#post-4617184
and
http://etivite.com/api-hooks/buddypress/trigger/apply_filters/bp_user_can_create_groups/
How can I use this filter to restrict group creation for a particular user role in BuddyPress? Currently, only admins are allowed to create groups and I also dont want to allow every user to be able to create groups.
I have added the following code to bp-custom.php but its not working
function create_groups1( $can_create, $restricted=false ) {
// maybe we don't want to override if it's restricted?
if ( ! $restricted ){
// get the logged in user's ID
$user_ID = get_current_user_id();
// some logic to determine if the current user can create a group
$user1 = new WP_User( $user_ID );
if ( !empty( $user1->roles ) && is_array( $user1->roles ) ) {
$r=$user1->roles[0];
}
if (current_user_can('read')) {
$r="subscriber";
}
if ( $r == "subscriber" || $r == "participant" ){
// we will return this allowing them to create groups
$can_create = true;
}
}
return $can_create;
}
add_filter( 'bp_user_can_create_groups', 'create_groups1', 10, 2 );

You need to write a function and attach it to the 'bp_user_can_create_groups' hook. Inside your function determine if you want the current user to be able to create groups and return $can_create accordingly.
function bp_user_can_create_groups( $can_create, $restricted=false ){
// maybe we don't want to override if it's restricted?
if ( ! $restricted ){
// get the logged in user's ID
$user_ID = get_current_user_id();
// some logic to determine if the current user can create a group
if ( user_can_create_group( $user_ID ) ){
// we will return this allowing them to create groups
$can_create = true;
}
}
return $can_create;
}
add_filter( 'bp_user_can_create_groups', 'bp_user_can_create_groups', 10, 2 );

Related

Restrict certain user roles from making purchases in Woocommerce

No matter how many ways I try, I can't get it to work. I must mention that I have used this snippet for other purposes, such as restricting the possibility of buying if the product contains a certain tag or a certain category.
But to do this based on the user role, I just haven't been able to pull it off. I remember this did work like a couple of months ago, but now, it just doesn't work anymore. Is there something I'm missing?
add_filter('woocommerce_is_purchasable', 'modo_catalogo_por_rol_usuario', 10, 2 );
function modo_catalogo_por_rol_usuario( $is_purchasable, $product ) {
$user = wp_get_current_user();
$catalog_roles = array('cliente_empresa_limited', 'cliente_modo_catlogo', 'administrator'); //add your user roles here
$roles = ( array ) $user->roles;
$is_purchasable = true;
if ( in_array( $catalog_roles, $roles ) ) {
$is_purchasable = false;
}
return $is_purchasable;
}
The following code will restrict the ability to purchase products based on user role. The user roles cliente_empresa_limited, cliente_modo_catlogo, and administrator will be able to purchase without restriction:
/* Restrict the ability to buy products based on the user role.
Roles 'cliente_empresa_limited', 'cliente_modo_catlogo',
and 'administrator' will be able to purchase */
function modo_catalogo_por_rol_usuario(){
$user = wp_get_current_user();
$user_meta=get_userdata( $user->ID );
$user_roles=$user_meta->roles;
if ( ( in_array( 'cliente_empresa_limited', (array) $user_roles ) ) || ( in_array( 'cliente_modo_catlogo', (array) $user_roles ) ) || ( in_array( 'administrator', (array) $user_roles ) ) ) {
return true;
}
else {
return false;
}
}
add_filter( 'woocommerce_is_purchasable', 'modo_catalogo_por_rol_usuario' );
Add the code above in functions.php file of your active child theme or active theme. It is tested and it works.
You should use array_intersect which will return an array of matches between two arrays. If the count is greater than 0, then it will be true.
add_filter( 'woocommerce_is_purchasable', 'modo_catalogo_por_rol_usuario', 10, 2 );
function modo_catalogo_por_rol_usuario( $is_purchasable, $product ) {
$user = wp_get_current_user();
$catalog_roles = array( 'cliente_empresa_limited', 'cliente_modo_catlogo', 'administrator' ); // add your user roles here.
$roles = (array) $user->roles;
if ( 0 < count( array_intersect( $catalog_roles, $roles ) ) ) {
$is_purchasable = false;
}
return $is_purchasable;
}

Contact Form 7 (CF7) radio button to change User Role on WordPress

I have a form setup for users to fill in after they register on my WordPress site. Ive setup the form using Contact Form 7, and have a radio button called radio-766 that has two options: subscriber and customer.
I want the user to pick one of these two options, then when they submit the form, it will change their user role.
Below is what I have so far... I've grabbed snippets from online and tried to create my own, but it isn't working. Any ideas on how I can get this to work?
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $posted_data['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->roles[ $index ];
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
Should I be including the form name or ID anywhere? Can't find info on this
Any help is so appreciated!
EDIT
I feel like there is nothing connecting this function to the CF7 form named "After LinkedIn", so I found this snippet of code, just not sure how to integrate and get working
if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
// Contact Form 7 version 3.9 removed $cfdata->posted_data and now
// we have to retrieve it from an API
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$formdata = $submission->get_posted_data();
}
} elseif (isset($cfdata->posted_data)) {
// For pre-3.9 versions of Contact Form 7
$formdata = $cfdata->posted_data;
} else {
// We can't retrieve the form data
return $cfdata;
}
// Check this is the user registration form
if ( $cfdata->title() == 'After LinkedIn') {
As per the Contact form 7 plugin author is_user_logged_in() will work on below to cases in form submission:
subscribers_only: true set in the additional setting in form. OR
Set WPCF7_VERIFY_NONCE to true using add_filter( 'wpcf7_verify_nonce', '__return_true' );
For more information click here.
Also, to change the user role you can do the following:
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
function tm_cf7_roles_posted_data( $posted_data ) {
$submission = WPCF7_Submission::get_instance();
$wpcf7 = WPCF7_ContactForm::get_current();
$formID = $wpcf7->id();
if ( $submission ) {
if( $formID == "YOUR-FORM-ID" ) {
if ( is_user_logged_in() ) {
$role = sanitize_key( $posted_data['radio-766'][0] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
// Getting the WP_User object
$user = wp_get_current_user();
// The user exists
if( $user && $user->exists() ) {
// Check if user already has that role or not
if ( !in_array( $role, (array) $user->roles ) ) {
// Remove all the previous roles from the user and add this one
// This will also reset all the caps and set them for the new role
$user->set_role( $role );
}
}
}
}
}
}
If you only want to add a new role to the user and retain the existing role then instead of set_role use below:
// Add a new role to the user while retaining the previous ones
$user->add_role( $role );
As for programatically setting a user role you can just take the user object and use the set_role() function to change their role to whatever you want as long as that role has been defined.Try this way
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $_POST['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->set_role($role);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );

Show WooCommerce products only to multiple authorized user roles

I'm trying to adapt "Completely hide products from unauthorized users in WooCommerce" answer code to also allow several custom user roles to view this products. I believe the best way to accomplish this is to expand the authorized user function to include this user roles.
This is the changes that I have tried to implement with no success. Can someone shine a light on how to proceed?
// Conditional function checking for authorized users
function is_authorized_user() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$caps = $user->allcaps;
if ( ( isset($caps['edit_product']) && $caps['edit_product'] ) ||
array( 'custom_user_role1', 'custom_user_role2', $user->roles ) )
return true;
} else
return false;
}
How to make it work for an array of user roles, instead of just one? Any help is appreciated.
As you have 2 arrays to compare:
your 2 custom roles (in an array)
the current user roles (that is an array)
you can use array_intersect() php function to make it work this way:
// Conditional function checking for authorized users
function is_authorized_user(){
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$caps = $user->allcaps;
if ( ( isset($caps['edit_product']) && $caps['edit_product'] ) ||
array_intersect( ['custom_user_role1', 'custom_user_role2'], $user->roles ) ) {
return true;
}
return false;
}
else {
return false;
}
}
It should work now for multiple user roles.

Limit page creations by user role functions.php

Is it possible to create a function via functions.php that restricts users from creating pages based on their user role? So for example, users with the user role "limited" can only create 5 pages.
When they reach this amount, the "Create new page" button must be gone or they have te receive a message that they reached there limit and have to contact there admin.
I know there is a plugin called Bainternet Posts Creation Limits but this is causing some problems.
Is something possible with a function?
EDIT:
Ok, I tried the code below but aint working and to be honest my coding skills are to poor to make something of it.
Anybody else any ideas?
You could try something like this:
Step 1: Upon user creation: give your user the capability 'publish_pages'
Step 2: Save in the user_meta how many pages the user has created.
Step 3: If the user has 5 or more pages remove the capability from this user.
STEP 1 & 2 (untested):
add_action( 'user_register', 'user_registered', 10, 1 );
function user_registered( $user_id ) {
// ADD CAPABILITY
$user = new WP_User( $user_id );
if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'YOURROLEHERE', $user->roles ) ) {
$user->add_cap( 'publish_pages');
update_user_meta($user_id, 'numberofpages', 0);
}
}
STEP 3 (untested):
add_action('publish_page', 'user_published_page');
function user_published_page(){
// GET CURRENT USER
$user_id = get_current_user_id();
$user = new WP_User( $user_id );
// CHECK ROLE
if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'YOURROLEHERE', $user->roles ) ) {
// GET NUMBER OF PAGES CREATED
$numberofpages = intval(get_user_meta($user_id, 'numberofpages', true));
if($numberofpages >= 5){
$user->remove_cap( 'publish_pages');
} else {
update_user_meta($user_id, 'numberofpages', $numberofpages + 1);
}
}
}

Get user role by ID WordPress

I need to somehow check someone's role with only their id.
I have found the current_user_can() check. But this only works for people that are logged in. How would I check for this if that user isn't the current user? I am using a phone order system but that uses the admin/specific account to order for other people.
To check if a user has a specific role, you have to get a list of their roles and see if the role is listed there.
Example function:
function user_has_role($user_id, $role_name)
{
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
return in_array($role_name, $user_roles);
}
Example usage:
$user_is_subscriber = user_has_role(get_current_user_id(), 'subscriber');
The info you need to know before you proceed:
You can't get user role by ID directly.
You can get all the roles a user is assigned to.
Let's get all the roles and check if the role you're interested in is there or now.
<?php
// Get the user object.
$user = get_userdata( $user_id );
// Get all the user roles as an array.
$user_roles = $user->roles;
// Check if the role you're interested in, is present in the array.
if ( in_array( 'subscriber', $user_roles, true ) ) {
// Do something.
echo 'YES, User is a subscriber';
}
hello try this optimal code
if (get_userdata($post->post_author)->roles[0] == 'your_specified_role'){
echo 'role exist';
}else{
echo 'role does not exist';
}
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
if ( in_array( 'administrator', $user_roles, true ) ) {
//echo User is a administrator';
}

Categories