How to force redirect for a new account in Wordpress? - wordpress

I want to force redirect on special page when a new account is created but it doesn't work. It always redirects to the WP dashboard.
function redirect_testtt($user_id) {
wp_redirect('http://www.example.com/me');
exit;
}
add_action('user_register', 'redirect_testtt');
How to force redirect for a new account please ?

Try:
// add action to after registration hook
add_action('user_register','after_reg');
// redirect users after registration
function after_reg() {
wp_redirect('http://google.com');
exit();
}

Related

Wordpress redirect to login page if user is not logged in

I need to force users to login before they can see browse my site. For new users Access should only be allowed to the my-account page which is my login url i.e. example.org/my-account
Below is what i've done so far
add_action('wp','force_register' );
function force_register() {
$loggedin = is_user_logged_in();
$pagename = get_query_var('pagename'); //get current page name
if(strcmp("my-account",$pagename) != 0){ //if the user is looking at any other page other than my-account page
if(!$loggedin){ //if the user is not logged in
wp_safe_redirect( "/my-account", 302 ); //redirect to the login page
exit;
}
}
The browser can't redirect and complains about too many redirects
What am I missing ?
Before WordPress does any routing, processing, or handling. It is run in the main() WP method in which the $query_args are passed to parse_request(), as well as when send_headers() , query_posts(), handle_404(), and register_globals() are setup. This is caused for your issue, You may use init instead of wp hook. more about wp hook.

How to Redirect Registration page to My Account page if user is logged in?

I have a custom registration page and working fine unless I am logged-in. When I am login and click on my registration page it takes me to the registration page but it is blank. I want it to redirect the user if he/she logged-in to the my account page.
You can do it like this in your header.php
$post = get_post();
if(is_user_logged_in()) {
if($post->slug == 'regitrate-slug-here'){
header( 'Location: /my-page-slug-here' );
}
}
or just paste the link in the if statement
if(is_user_logged_in()) {
My page
} else {
Register
}
To Redirect custom Registration page to My Account page you should try small javascript something like this.
Write this code in your php file that you have created for registration.
if(is_user_logged_in()) {
echo("<script>location.href = '".home_url('my-account')."'</script>");
exit;
}

How to disable Woocommerce login redirect if on Checkout Page

I have some code in my WordPress functions.php file to do a redirect after a successful woocommerce login. It works great, but I'm wondering how I can disable that redirect from running on the checkout page?
If a user logs into their account while on the checkout page they are getting redirected off the checkout page just before they have the chance to fill in their credit card data which isn't a great experience.
According to https://docs.woocommerce.com/document/conditional-tags/ conditional query tags like is_checkout() won't work in the functions.php file because You can only use conditional query tags after the posts_selection action hook in WordPress. And, unfortunately, apparently the functions.php file gets run before that.
What's my best option to solve this?
Here's the code I currently have running (the one I want to disable on checkout page):
function woo_login_redirect( $redirect_to ) {
$redirect_to = get_permalink(70241);
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');
This is me trying to avoid the redirect if on checkout page (it doesn't work. It just redirects to post id 70241):
function woo_login_redirect( $redirect_to ) {
if (is_checkout()){
$redirect_to = '#';
} else {
$redirect_to = get_permalink(70241);
}
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');

change homepage for logged-in users in wordpress

In wordpress i want a different homepage for logged-in users and those who are logged-out (simple changing the redirect url when somebody clicks on the title of my website). Is there a way of doing this by adding a code snippet in my themes functions.php? I tried:
add_filter('get_the_permalink','my_permalink_redirect');
function my_permalink_redirect($permalink) {
if( is_user_logged_in() ) {
global $post;
if ($post->ID == 1) {
$permalink = 'http://localhost/homepage-for-logged-users';
}
}
return $permalink;
}
unfortunately it doesn't work and it would be unhandy to change the function from localhost to my domain name when i upload the site to a live hosting. can somebody give me some advice? Thanks!
edit:I got it working with the pluging "login with ajax" where I could define redirects after logins and I putted this snippet in my functions.php.
// Redirect users who arent logged in and on a 404 to the home page for logged out users
function login_redirect() {
// Check to see if user in not logged in and on the home page for logged users
if(!is_user_logged_in() && is_404()) {
// If user is, Redirect to home page for logged out users.
wp_redirect(home_url('home-logged-out'));
exit;
}
}
// add the block of code above to the WordPress template
add_action( 'wp', 'login_redirect' );
*I made the pages for logged-in users private so logged-out users will see 404. Not a very clean method, but it works for now...
Set your default home page to the page logged in users should see.
Add this code to functions.php:
add_action('init', 'redirect_user');
// for users not logged in
function redirect_user(){
if( !is_user_logged_in() ) {
wp_redirect( 'http://www.YourSite.com/SomePage');
exit;
}
}
Logged in users will see what they should see, and those not logged in will be redirected.

Session Login in wordpress

I have a site www.example.com and a wordpress blog at www.example.com/blog.
I want my user to login at www.example.com and $_SESSION['USERNAME'] is passed to www.example.com/blog.
I just want to know, how can i automatically login the end user to wordpress blog, once they login to my main site.
For now i am passing $_SESSION['USERNAME'] and using a plugin external database login, which I have linked to my main site database.
Any function that is used to login to wordpress using session username will be helpful.
Wordpress don't use $_SESSIONyou need to code a plugin or to add some code to your functions.php to do this - cf http://www.frank-verhoeven.com/using-session-in-wordpress/
You should add something like this :
function init_sessions() {
if (!session_id()) {
session_start();
}
}
add_action('init', 'init_sessions');
NB: there is also a plugin to manage Sessions with Wordpress, maybe you can use/hack this http://wordpress.org/plugins/wp-session-manager/
I have been reading on this. Here is what I have found as I am in the same issue. In your wp-config.php file at the top add:
add_action('init', 'myStartSession', 1);
function myStartSession() {
if(!session_id()) {
session_start();
}
}
HOWEVER, " The data stored in the session doesn’t go away when the user logs out or logs into a different account. For that you need to destroy the session. ..." (exerpt from this nice write up). It explains you also need to:
function myEndSession() {
session_destroy ();
}
if you can get wp user_id you could do something like this:
function init_sessions() {
if (user_logged_on_site) {
#session_start();
$user_id = $_SESSION['wp_user_id']; //this has to be set when the user logs on your website
wp_set_auth_cookie( $user_id); //log in the user on wordpress
}
}
add_action('init', 'init_sessions');
Documentation on wp_set_auth_cookie http://codex.wordpress.org/Function_Reference/wp_set_auth_cookie

Resources