I try to find a way to know when a user is created in Wordpress, can anyone help me?
What I want is a function so when an admin is registered the fuction do X thing. What is X thing? I am going to take the data of that user to create an account on another platform.
Summary: I want to know when an administrator is created with a function to run another function.
There are multilple hooks you can use. Here is a nice overview : https://usersinsights.com/wordpress-user-registration-hooks-visualized/
I think user_register is the right one for you:
add_action( 'user_register', function ( $user_id ) {
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
// you now have the role and can call your function after checking if it is admin
} );
Related
I have a custom function that changes a user status on another system.
I would like to run this function with automatewoo
however, I don't know how to pass the username of the customer to the function when using this.
If I run the function through web hooks - I just use get_current_user()
but this doesn't work when running through automate woo.
any help would be appreciated
thanks M
the original code works well when run using a hook as follows:
add_action( 'woocommerce_thankyou', 'update_user_status'); function update_user_status(){ $user = wp_get_current_user();
however when I replace that piece of code with what automate woo example look like :
function update_user_status(){ $user = $workflow->data_layer()->get_user();
Then the user status is not updated - because I am not getting the customer's user details
How do I pass the value of wp_get_current_user() from automatewoo to the custom function?
Wordpress PHP:
I am in need of a small php script that allows me to check for the userrole of the active logged in user. And to output it as a class item.
I have assigned User Roles via Ultimate Member - if that has anything to say.
Example:
A user is a member of a role, named: "storeowner".
I would like to add that to a div as a shortcode of sorts.
Is there a quick and dirty solution out there ?
Sorry if this has been asked and answered, but I haven't been able to find it.
You can create a helper function to return a space separated list of user roles "role1 role2 role3":
function my_get_role_classnames($uid) {
$user_meta = get_userdata($uid);
$user_roles = $user_meta->roles;
return implode(" ", $user_roles);
}
Then use like so:
class="<?php echo my_get_role_classnames(1); ?>"
I was unable to login to wp-admin "You don't have sufficient privileges" so I added this code to my themes functions.php
/*
* Add your own functions here. You can also copy some of the theme functions into this file.
* Wordpress will use those functions instead of the original functions then.
*/
function codelight_all_permissions( $allcaps, $cap, $args ) {
$allcaps[$cap[0]] = true;
return $allcaps;
}
add_filter( 'user_has_cap', 'codelight_all_permissions', 0, 3 );
Then I was able to get in as site admin, I went to users and noticed there are no roles assigned. In the database I have level 10 and a:1:{s:13:"administrator";s:1:"1";} assigned.
If I create a new user I cannot choose a role for that user from the dropdown. I only get the option "No role for this site". Does anyone know how I can troubleshoot this issue?
Basiclly, I must Remove the next roles whenever the admin is creating a new user and clicks on the dropdown menu tu display all the available roles:
Contributor
Editor
Unapproved User
Author
It should only display Subscriber and Administrator.
Also I must change the name of Subscriber to something different, where is this code? This problem would seem to me like there was a plugin to it.
To remove a role you can use
<?php remove_role( 'editor'); ?>
and to rename a role add the following php t your functions.php file
function change_role_name() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
//You can replace "administrator" with any other role "editor", "author", "contributor" or "subscriber"...
$wp_roles->roles['administrator']['name'] = 'Owner';
$wp_roles->role_names['administrator'] = 'Owner';
}
add_action('init', 'change_role_name');
(The above is from here
There is a function called remove_role that WordPress has in its PHP API. you use it like this:
<?php remove_role('author'); ?>
author could be replaced with any role you want. The way you want to use this command is different than most WP commands. This is because it removes the role from the DB so any future times, the role is already removed. I would recommend making a custom WP plugin that just removes the roles you want on activation.WP Plugin Help
I need to check if a user have capabilities to do something in the initialization of a plugin.
I tried with current_user_can() function, and WP_User class, but dont work in these scope.
The question is: I need to check if a user have capabilities in wp-admin, to let he see a specific menu or just to load the content of the plugin.
But how? Both methods above generates a php error.
You should hook your function to run after plugins have loaded
function my_special_function() {
if ( current_user_can( 'do_whatever' ) )
// do your thing
}
add_action( 'plugins_loaded', 'my_special_function' );
Source: http://core.trac.wordpress.org/ticket/23861