Change user role via URL [WordPress] - php

I'm creating plugin which will register all new users as role = unverified and stop user from login until email verification. I want to send url on their email which will change the current unverified role to author. I know how to send email etc all of that in WordPress but can't figure out how to create url which will change the role. I'm using custom ajax form to login,register and lostpassword because of it i'm unable to use pie register and register-plus-redux plugins.
Current code
function add_roles_on_plugin_activation() {
add_role( 'custom_role', 'Unverified', array( 'read' => true, 'level_0' => true ) );
}
register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' );
function remove_roles_on_plugin_deactivation() {
remove_role( 'custom_role' );
}
register_deactivation_hook( __FILE__, 'remove_roles_on_plugin_deactivation' );
add_filter('pre_option_default_role', function($default_role){
return 'custom_role';
return $default_role;
});
function error_email_verify() {
$user = wp_get_current_user();
if ( in_array( 'custom_role', (array) $user->roles ) ) {
$logout_url = wp_login_url().'?mode=emailverify';
wp_logout();
wp_redirect( $logout_url );
exit();
}
}
add_action('wp_loaded', 'error_email_verify');
function my_login_message() {
if( $_GET['mode'] == 'emailverify' ){
$message = '<p id="login_error"><b>Verify your email.</b></p>';
return $message;
}
}
add_filter('login_message', 'my_login_message');

Here is what I would do. Basically I would generate a link that would have parameters attached to it something like this:
Generate Link
$username = 'user_login';
$hashcode = sha1(md5(md5("hacaak".$username."aalog")));
$link = get_home_url().'/?a='.$hashcode.'&b='.$hashcode.'&u='.$username.'&c='.$hashcode.'';
Output of link
[http:yourwebsite.com/a=fe440709d341e7b4994636b12e556aa7f23bb9ce&b=fe440709d341e7b4994636b12e556aa7f23bb9ce&u=jack&c=fe440709d341e7b4994636b12e556aa7f23bb9ce][1]
When the user clicks on this link in the email you just sent them use this method to catch the GET parameter coming into the site:
function to get the variables from url
function catch_email(){
if(isset($_GET['a']))
{
$username = sanitize_user($_GET['u']);
$user=get_user_by( 'login', $username );
$user_id = $user->ID;
$user_role = new WP_User($user_id);
$user_role->remove_role( 'unverified' );
$user_role->add_role( 'author' );
$url = get_home_url().'/Login?mode=emailverify';
wp_redirect($url);
}
}
add_action('get_header', 'catch_email');

Related

php redirect with custom user roles set and working but redirect no

I have created code to add custom user rules. It works. However when I try to tie in the code for redirecting an already logged in user based on one test role, then it doesn't work. What am I missing in this in order to have it redirect, based on a user's role. Here is the code:
add_action('init', 'cloneRoleCompany');
function cloneRoleCompany(){
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$adm = $wp_roles->get_role('subscriber');
//Adding a 'new_role' with all admin caps
$wp_roles->add_role('company-admin', 'Company Admin', $adm->capabilities);
$wp_roles->add_role('company', 'Company', $adm->capabilities);
$wp_roles->add_role('user', 'User', $adm->capabilities);
}
function redirect_from_front_page() {
$redirect_url = '/user-dashboard';
$expected_role = 'user';
if( is_front_page() ) {
return;
}
if( is_user_logged_in() ) {
return;
}
$user = wp_get_current_user();
$roles = $user->roles;
if ( in_array( $expected_role, $roles ) ) {
wp_safe_redirect( $redirect_url );
exit;
}
}
You have condition is_user_logged_in() which prevents to get into the redirect part of code, only logged in user have a role.
if( is_user_logged_in() ) {
return; // when user is logged in this is last instruction
}
// code below will not get executed
$user = wp_get_current_user();
$roles = $user->roles;

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.

Wordpress - Logout redirection based on role

I'm trying to determine how I can redirect users upon logout, to a URL defined by their role. Simply put, I want to redirect admins (as well as editors) that logout to a different URL, than subscribers / privileged users.
I'm using the following code to redirect users at logout right now, but this redirects everyone. Any insight as to how I can have a different redirect based on their account role, would be great!
/**
* Redirect to custom login page after the user has been logged out.
*/
public function redirect_after_logout() {
$redirect_url = home_url( 'member-login?logged_out=true' );
wp_safe_redirect( $redirect_url );
exit;
}
add_action( 'wp_logout', array( $this, 'redirect_after_logout' ) );
Thanks!
Please check the doucumentation https://developer.wordpress.org/reference/functions/current_user_can/ how to check if current user has the specified capability.
Here is a sample:
if (!current_user_can('edit_posts') && !is_admin()) {
$redirect_url = site_url();
} else {
$redirect_url = '/wp-login.php';
}
function wpdocs_redirect_after_logout()
{
global $redirect_url;
wp_safe_redirect($redirect_url);
exit;
}
add_action('wp_logout', 'wpdocs_redirect_after_logout');
Im not sure didnt test but you can use user roles with if else statement
function redirect_after_logout() {
if (!current_user_can('manage_options')) {
$url = '/';
} else {
$url = 'member-login?logged_out=true';
}
$redirect_url = home_url( $url );
wp_safe_redirect( $redirect_url );
exit;
}
add_action( 'wp_logout', array( $this, 'redirect_after_logout' ) );

User login by email in wordpress is not working

I have been trying to enable the user login by email. I found at wordpress codex and some few blogs the way to write the following code in functions.php
add_action( 'wp_authenticate', 'wp_authenticate_by_email' );
function wp_authenticate_by_email( $username ) {
$user = get_user_by( 'email', $username );
if ( empty( $user ) ) {
return;
}
return $user->user_login;
}
But it does not work for me. I found it is remained as earlier.
I had a similar issue but got this to work:
function my_authenticate_by_email( $user, $username, $password ) {
//try to get user by email
$user = get_user_by( 'email', $username );
//validate we got a user.
if ($user && !is_wp_error($user))
{
//use normal auth
return wp_authenticate_username_password(null, $user->user_login, $password);
}
//continue to next filter, like normal.
return null;
}
add_filter( 'authenticate', 'my_authenticate_by_email', 0, 3);

How to redirect new WordPress multisite user inside their admin panel after they log in?

Here is the code in my functions.php that redirects users (to their new "Sample Page") when they log in:
function admin_default_page() {
return '/wp-admin/post.php?post=2&action=edit';
}
add_filter('login_redirect', 'admin_default_page');
The problem is that the url to which we redirect is missing the users site url.
For example, if a user creates a site "Bobs Trucks", it redirects them to
example.com/wp-admin/post.php?post=2&action=edit (does not work)
instead of
example.com/bobstrucks/wp-admin/post.php?post=2&action=edit
I've tried all the WordPress functions to get the full url needed (example.com/bobstrucs/url-to-redirect-to) but have not succeeded.
I'm trying a rather hackish solution where I get the user ID form the logged in user, and get all the blogs the user has (= just one, "Bobs Trucks"):
function admin_default_page() {
global $current_user;
get_currentuserinfo();
$blogs = get_blogs_of_user( $current_user->ID);
foreach($blogs as $blog) { $userblog = $blog->siteurl; }
return $userblog .'/wp-admin/post.php?post=2&action=edit';
}
add_filter('login_redirect', 'admin_default_page');
But this one has the problem of not working the first time user logs in.
Thank you very much!
Try something like this:
function admin_default_page() {
if ( is_user_logged_in() && is_admin() ) {
$blog_id = get_current_blog_id();
$blog = get_blog_details( array( 'blog_id' => $blog_id ) );
$location = '/' . $blog->blogname . '/wp-admin/post.php?post=2&action=edit';
wp_redirect( $location );
exit;
}
}
add_filter( 'login_redirect', 'admin_default_page' );
Refs:
http://codex.wordpress.org/Function_Reference/get_blog_details
http://codex.wordpress.org/Function_Reference/wp_redirect

Categories