i am trying to redirect the user after login/signup to profile page but not sure why it is not putting user nickname/username in url.
$current_user = wp_get_current_user();
function
aloginuser( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
wp_redirect( "/profile/$current_user->user_login" );
exit;
}
add_action( 'user_register', 'aloginuser' );
It need to start a redirect to: site.com/profile/username
You are passing $current_user->user_login as string "$current_user->user_login" and not the value stored in the variable. You have to write it like this:
wp_redirect("/profile/".$current_user->user_login);
Also, it is necessary to move the $current_user = wp_get_current_user(); inside the function.
Related
I am trying to redirect my wordpress users to a certain page when they acces the front page.
"Henk" has a page named "henkpage" and "John" has a page named "johnpage".
If the current user is Henk and he's on the front page, then i want that user to be redirected to mydomain.com/henkpage. And same with John: If current user is John and on the frontpage, then redirect to /johnpage. Could anyone help me?
I found this code that might have some usefull lines in them.
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_front_page() && is_user_logged_in() ) {
$current_user = wp_get_current_user();
$url = wpum_get_profile_url( $current_user );
wp_redirect( $url );
exit;
}
}
The code you posted should work for your purposes, but you'll need to modify this line
$url = wpum_get_profile_url( $current_user );
and set $url to $current_user->display_name.'page' or whatever you'd like it to redirect to.
I have the code above into functions file of my theme. I did this code to logout the user "messenger" when another page that not use the template page_forms is requested.
But, when I access some page that use this template, I'm being redirected to the homepage and the session is closed. Which makes me believe that the is_page_template statement is not working properly.
Does anyone have any tips?
function messenger_session(){
$current = wp_get_current_user();
if( isset($current->user_login) && $current->user_login == 'messenger' && !is_page_template('page-forms.php') ):
$redirect_to = get_home_url();
wp_logout();
wp_safe_redirect( $redirect_to );
exit;
else:
return;
endif;
}
add_action('init','messenger_session');
UPDATE
After research, I solve the problem changing 'init' from add_action to 'template_redirect'. This change make the user messenger stay logged in on page-forms.php.
But, the second step is make logout of this user if any other template is required. So, to make this happen, I make some changes in my code:
function messenger_login(){
$current = wp_get_current_user();
if( is_page_template('page-forms.php') && !isset( $current->user_login ) ) :
$username = 'messenger';
$user = get_user_by( 'login', $username );
wp_clear_auth_cookie();
wp_set_current_user( $user->ID );
wp_set_auth_cookie( $user->ID );
return;
elseif( !is_page_template('page-forms.php') && isset( $current->user_login ) && $current->user_login == 'messenger' ):
$redirect_to = get_home_url();
wp_clear_auth_cookie();
wp_safe_redirect( $redirect_to );
die;
else:
// do nothing
endif;
}
add_action('template_redirect','messenger_login');
And the problem now is that after messenger login on page-forms.php, the logout happens automatically withou respect the second statement of the function where the template needs not be page-forms.php to fire the logout (without redirect to home -- is crazy!).
When I comment the function wp_clear_auth_cookie, the problem stop. But the function lose your porpouse.
That function is_page_template is working correctly just make sure you're using correct path of your page_form template if that template is inside any folder like folder/page-form.php then you must need to use that full path after your theme folder suppose if your template is inside templates folder then you must need to pass templates/page-forms.php within is_page_template function
Let me know if this helps you
This is a query based around WooCommerce Membership and Subscriptions.
I must add that I'm also trying to decide if it's good UX to do what I'm doing.
There are many solutions to redirecting users after they log in but I have a situation where I want to redirect a user with the role of 'subscriber' when they click on specific links to pages that describe and allow you to become a member. So although I don't want to hide 'join now' etc I just want those to redirect to the my-account page.
Again there are various roles and redirect plugins but none seem to help in this specific scenario. So the source of the code I've used is here: SOURCE and I'm looking to do something like this:
function eks_redirect_users_by_role() {
global $post;
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if ( 'subscriber' === $role_name && $post->ID == 47145) {
wp_redirect( '/my-account' );
}
}
add_action( 'admin_init', 'eks_redirect_users_by_role' );
So if the user role is a subscriber and they try and visit the page idea it's redirected.
At the current time, it does fall back to a product page which says 'you already have a membership' but it's multiple steps to arrive.
This might be more useful and proper way to achieve your request.
function redirection_based_user_role(){
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) {
wp_redirect( home_url( '/my-account' ), 301 );
exit;
}
}
add_action( 'template_redirect', 'redirection_based_user_role' );
Hope this works.
I was able to achieve want I wanted to the following way:
function eks_redirect_user() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) { ?>
<script>
function redirectPage() {
window.location = "/my-account/";
}
redirectPage();
</script>
<?php
exit;
}
}
add_action('wp_footer', 'eks_redirect_user' );
The issue is it's a fairly untidy solution as the redirect feels odd. I'm not sure if using wp_redirect would work better. I decided to do was just disable the button on the page with the main membership information rather than redirecting every call to action to the account page which seems like a more elegant solution.
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;
}
}
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