I found the answer from Nathan Powell but I can not apply this function to a specific user role. I want this function applies only for user "author" role.
I've been using :
if ( !current_user_can( 'author' )):
and I'm wondering how to place it.
Try below code:
function wpse_199918_wp_editor_settings( $settings, $editor_id ) {
$current_user = wp_get_current_user();
if ( $current_user->roles[0] === 'author' ) {
$settings['tinymce'] = false;
$settings['quicktags'] = false;
$settings['media_buttons'] = false;
}
return $settings;
}
add_filter( 'wp_editor_settings', 'wpse_199918_wp_editor_settings', 10, 2 );
Related
I have created a custom role that only has access to certain pages and their children/parents. I have used map_meta_cap filter to solve this. However even though it goes through my function correctly it will not function properly, return ['do_not_allow']; is not working as intended and I don't know why, it seemingly does not do anything.
//Role cap for different pages
function staby_map_meta_cap( $caps, $cap, $user_id, $args ) {
// // If the capability being filtered isn't of our interest, just return current value
if ( in_array($cap, ['edit_pages']) ) {
// First item in $args array should be page ID
if (!staby_role_can_edit( $user_id, $args[0] ) ) {
// User is not allowed, let's tell that to WP
return ['do_not_allow'];
}
}
// Otherwise just return current value
return $caps;
}
add_filter( 'map_meta_cap', 'staby_map_meta_cap', 10, 4 );
//See if role can edit correspondent page
function staby_role_can_edit( $user_id, $page_id) {
$page = get_post( $page_id );
// let's find the topmost page in the hierarchy
while( $page && (int) $page->parent ) {
$page = get_post( $page->parent );
}
if ( ! $page ) {
return false;
}
$user = new WP_User($user_id);
if ($user->allcaps['pages_id']) {
$user_pages = $user->allcaps['pages_id'];
if (!in_array($page->ID, $user_pages)) {
return false;
}
}
return true;
}
The only thing I have noticed is that the map_meta_cap filter runs multiple times, and the $args variable is always empty but it somehow still retrieves the $args[0] (page id) in my custom function staby_role_can_edit( $user_id, $page_id ) (but when I call it in that function it's null?). I have no idea why it's not working at all.
Any help is very appreciated! Thanks.
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;
}
I have created a custom user taxonomy called "forums" and I'd like to find a user which has both the "forum-leader" role as well as the same "forum" as the logged in user.
function find_forum_leaders() {
ob_start();
if( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$args = array(
'role' => 'um_forum-leader',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
foreach ( $users as $user ) {
$my_forum = get_current_user_forum();
$forum = the_terms($user, 'forums');
if ($my_forum == $forum) {
return $user->display_name;
}
}
}
$output = ob_get_clean();
return $output;
}
add_shortcode('user_leaders', 'find_forum_leaders');
I created a function to find the logged in user's forum here:
function get_current_user_forum() {
ob_start();
if( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$forum = the_terms( $current_user, 'forums');
echo $forum;
}
$output = ob_get_clean();
return $output;
}
In my find_forum_leaders function, my output isn't displaying correctly. I want to display the name and email of the user only if they match the logged in user's forum, but instead it is simply outputting the user's forum. I have a very rudimentary knowledge of how php works so I could use some help figuring this out. Also I probably could use to clean up this code, so any tips in that department would also be appreciated.
Thanks!
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' );
I've created a custom WooCommerce checkout field with Woothemes Checkout Field Editor labeled "po_number". I would like the PO Number checkout field to only display for the user role "distributor".
So far I've been unsuccessful in overriding the checkout fields. I'm using Wordpress 4.5.1 / Woocommerce 2.5.5. Here's the code I've placed in my child theme's functions.php. I've also tested to make sure it is not a theme conflict.
Any help is greatly appreciated.
This is my code:
function custom_override_checkout_fields( $fields ) {
if ( ! current_user_can( 'distributor' ) && isset( $fields['billing']['po_number'] ) ) {
unset($fields['billing']['po_number']);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
The current_user_can() function is related to capabilities of the user roles, but not to detect the user roles themselves. For that reason is not working in your code.
You need to set a conditional function for this purpose (user roles):
function is_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) ) {
$user = get_userdata( $user_id );
} else {
$user = wp_get_current_user();
}
if ( empty( $user ) ) {
return false;
}
if ( in_array( $role, (array) $user->roles ) == 1) {
return true;
} else {
return false;
}
}
Then in your code you can use that function:
function custom_override_checkout_fields( $fields ) {
if ( !is_user_role( 'distributor' ) && isset( $fields['billing']['po_number'] ) ) {
unset($fields['billing']['po_number']);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
This should work in your code.