WordPress - remove capabilities based on user id - php

I current have this code written:
add_action( 'init', 'hide_user_caps' );
function hide_user_caps() {
$user_id = 1;
$user = new WP_User( $user_id );
$user->remove_cap( 'edit_posts' );
}
However, this doesn't seem to be doing anything. Any ideas?
Thank you

If you want to specifically deny a capability from a user (not a role), you need to use the add_cap()[1] method and specify that the $grant parameter is false. [2].
$user_id = 1;
$user = new WP_User( $user_id );
$user->add_cap( 'edit_posts', false );
[1]: WP_User::add_cap()
[2]: The answer

Related

Create user on order status completed action hook in WooCommerce 3+

I am trying to create a new user within the woocommerce_order_status_completed hook.
add_action( 'woocommerce_order_status_completed', 'custom_woocommerce_order_status_completed', 10, 1 );
function custom_woocommerce_order_status_completed( $order_id )
{
$password = wp_generate_password( 16, true );
$user_id = wp_create_user( 'usertest#gmail.com', $password, 'usertest#gmail.com' );
}
$user_id returns an actual ID. It appears to create the user but when i look at the backend the user is not there. I even check the database for the user id and it's not there.
If i call the same function on the action woocommerce_after_register_post_type it creates the user.
Any idea what could be causing this issue?
The following code uses Woocommerce wc_create_new_customer() dedicated function instead:
add_action( 'woocommerce_order_status_completed', 'action_on_order_status_completed', 20, 2 );
function action_on_order_status_completed( $order_id, $order ){
$password = wp_generate_password( 16, true );
$user_name = $user_email = 'usertest#gmail.com';
// $user_name = $user_email = $order->get_billing_email();
$customer_id = wc_create_new_customer( $user_email, $user_name, $password );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
It looks like your function name does not match the one call within your hook:
custom_woocommerce_order_status_completed

Change role of all users with email address from specific domain upon custom WordPress plugin activation

I'd like to change the role of all users with a specific domain name (#example.com) to a custom role when my plugin is activated.
I found some code for doing it when a user registers and attempted to adapt it to my needs but it doesn't seem to work. Nothing happens when I activate the plugin. The roles don't change and I don't get any errors popping up so I'm not quite sure what I'm doing wrong.
I'm still learning PHP so please forgive me if this doesn't make sense.
Here's my code:
function set_role_by_email( $user_id ){
$user = get_user_by( 'id', $user_id );
$domain = substr(
strrchr(
$user->data->user_email,
"#"
), 1
); //Get Domain
$custom_role_domains = array( 'example.com' );
if( in_array( $domain, $custom_role_domains ) ){
foreach( $user->roles as $role )
$user->remove_role( $role ); //Remove existing Roles
$user->add_role( 'custom_role' ); //Add role to user
}
}
register_activation_hook( __FILE__, 'set_role_by_email' );
You could try this code and see if it works:
function set_role_by_email()
{
$users = get_users();
foreach ($users as $user) {
if (strpos($user->user_email, '#example.com')) {
foreach ($user->roles as $role) {
$user->remove_role($role);
}
$user->add_role('custom_role');
}
}
}
register_activation_hook(__FILE__, 'set_role_by_email');
Make sure this code is placed in the main plugin file.

Change Wordpress user role on gravityforms submission

I am using Gravityforms along with User registration add-on and have a form which when submitted should change the role of the current user to a new role without any underlying conditions.
Using the gravity\forms docs https://docs.gravityforms.com/gform_user_updated/#1-update-user-role and trying this:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // update 'role' to the name of the desired role
}
But its not working! Does anyone have any idea why this is incorrect or any other modifications to the code?
There's a couple things I see with your code when comparing it to the Gravity Forms doc.
Here's your code with some added comments:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user; // you probably don't need this
get_currentuserinfo(); // you probably don't need this
$user_id = $current_user->ID; // $user_id should already be a numeric value passed in to the function containing the logged in user's ID so you shouldn't need to do this. You're resetting the $user_id variable here to whatever is being pulled out of the get_currentuserinfo() function, and I'm guessing that's the problem
//I would get rid of this echo and if statement
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // the word "role" here needs to be the role name
}
I think you can simplify it some. Try this instead:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
$user = new WP_User( $user_id );
$user->set_role( 'new_role_name_here' ); // Add an existing role here to update the user too
}

Why it doesn´t work?

I´m writing that to give permission automatically an user to post when user have finished the purchase in woocommerce, but not before to buy,
but I don´t know how to do it, Wordpress is something new for me and php too. The following code doesn´t fail, but it doesn´t work. Can you help me, please?? By the way,
I´m writing this code on functions.php, I guess that´s the proper file.
Thank you very much in advance.
add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){
$order = wc_get_order( $order_id );
$user = $order->get_user();
if( isset($user) ){
$user = new WP_User( $user_id );
$user->add_cap( 'publish_posts' );
}
}
$user_id is not defined.
WC_Order::get_user() returns a WP_User so a new WP_User is not necessary.

Why this redirection isn't working?

I'm fairly new to Wordpress so not sure what I'm doing wrong. On my Wordpress site, users are able to create custom post types (products). I'm looking to disallow access to a page like this: example.com/post-a-listing by redirecting users to another page if they didn't create at least one product. Here's what I've tried but this isn't working..
function yoursite_user_has_posts($user_id) {
$result = new WP_Query(array(
'author'=>$user_id,
'post_type'=>'product',
'post_status'=>'publish',
'posts_per_page'=>1,
));
return (count($result->posts)!=0);
}
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page('post-a-listing') && ! yoursite_user_has_posts($user_id) ) {
wp_redirect( 'https://example.com/', 301 );
exit;
}
The issue for the function is $user_id actually , from where you can get the $user_id with out referencing it?
Use get_current_user_id() inside the function to get the current User ID.
function redirect_to_specific_page() {
$user_id = get_current_user_id();
if ( is_page('post-a-listing') && ! yoursite_user_has_posts($user_id) ) {
wp_redirect( 'https://example.com/', 301 );
exit;
}
}

Categories