Limit page creations by user role functions.php - 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);
}
}
}

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

How to redirect a user off a page by user role?

A lot of these guides are 'redirect after login' I have mulitple user types with different levels of access, I'd like to write some code which if a 'Subscriber' role goes onto the page (Which they do not have access it'll redirect them.
Here's one of the many code snippets I've tried, but can't get it to work! Help :)
function consultant_page_redirect() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if (('subscriber' === $role_name ) &&( is_single( array(2864, 2630)) ) ) {
wp_redirect('/trial-user-page/');
}
}
function consultant_page_redirect( $user_login, $user ) {
$role_name = $user->roles[0];
if (('subscriber' === $role_name ) &&( is_single( array(2864, 2630)) ) ) {
wp_redirect('/trial-user-page/');
}
}
add_action('wp_login', 'consultant_page_redirect', 10, 2);
Add this snippet code on child theme functions.php

Wordpress Author box Control Function

Hello friends,
i need little help in Wordpress. I am trying to hide the authorbox that appears under the post for specific user only.
Example if post i am looking into is posted by admin, then i want to hide the authorbox under post content that is posted by admin, but should show for all other users ? i tried different functions but not able to success on this.
I want to completly hide the authorbox, i know it can be done with css code that is
.author-box { display:none;}
but this hides the overall authorbox of complete them, i just want to hide the authorbox on the posts that are made by admin ?
i am using genesis framework, so please suggest if any help you can make here.
Thanks
In your theme's functions.php file, add something like this:
function remove_my_post_metabox() {
global $post;
// If the post author ID is 1, then remove the meta box
// You would need to find the ID of the author, then put it in place of the 1
if ( $post->post_author == 1) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
If you need to find out the role of the author, and truly do it based on the role (admin, for example), then it would be more like this:
function remove_my_post_metabox() {
global $post;
// If there's no author for the post, get out!
if ( ! $post->post_author) {
return;
}
// Load the user
$user = new WP_User( $post->post_author );
// Set "admin" flag
$is_admin = FALSE;
// Check the user roles
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
if ( $role == 'administrator' ) {
$is_admin = TRUE;
}
}
}
// Lastly, if they ARE an admin, remove the metabox
if ( $is_admin ) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );

Enable group creation for specific user role in BuddyPress

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

Categories