WordPress is_page_template not working right init action - php

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

Related

How to redirect after login to BuddyPress profile in WordPress

I run WordPress 5.1.1 with BuddyPress plugin.
Basically what I want to do is, redirect the user from custom login link to their profile page in BuddyPress.
I have read and check almost all codes provided on Stackoverflow for my similar query but non one has worked on my site.
The only code worked it's below but it has one problem for my site settings and setup.
function bp_help_redirect_to_profile(){
global $bp;
if( is_user_logged_in() && is_front_page() ) {
bp_core_redirect( get_option('home') . '/members/' .
bp_core_get_username( bp_loggedin_user_id() ) . '/profile' );
}
}
add_action( 'get_header', 'bp_help_redirect_to_profile',1);
The problem is, when I want to redirect on homepage of the website it's keep redirecting me on the BuddyPress profile. Categories and post sections are loading correctly.
So, what i need is, when the login flow redirect user on their BuddyPress profile page, after when user hit the homepage of the site to load the homepage of the site and not, the BuddyPress.
I hope someone can help and tweak the function in this way.
Thank you
add_filter( 'bp_login_redirect', 'bpdev_redirect_to_profile', 11, 3 );
function bpdev_redirect_to_profile( $redirect_to_calculated, $redirect_url_specified, $user )
{
if( empty( $redirect_to_calculated ) )
$redirect_to_calculated = admin_url();
//if the user is not site admin,redirect to his/her profile
if( isset( $user->ID) && ! is_super_admin( $user->ID ) )
return bp_core_get_user_domain( $user->ID );
else
return $redirect_to_calculated; /*if site admin or not logged in,do not do
anything much*/
}
Tested Code put in your active theme function file
You can use wp_login action for that. Please check following example. bp_core_get_user_domain() will fetch the profile URL and after successful login user will be redirected to that profile URL.
function wpso_take_to_profile( $user_login, $user ) {
if ( function_exists( 'bp_core_get_user_domain' ) ) {
$url = bp_core_get_user_domain( $user->ID );
wp_redirect( $url );
die();
}
}
add_action('wp_login', 'wpso_take_to_profile', 10, 2);

redirection of login form in wordpress

I'm using wp_login_form() on a site at the moment, and when you log in correctly it redirects the user to the appropriate URL.
Unfortunately if you make a mistake in the username/password it forwards you to wp-login.php, which shakes its box at the user. And disrupts consistent brand experience, like.
So does anybody know a way to make it not do that?
Try this :
This will let you include the header and footer of the theme at login page
add_action( 'login_head', 'wp_custom_login_header' );
function wp_custom_login_header() {
wp_enqueue_style( 'wp-custom-login' );
do_action('wp_custom_login_header_before');
get_header();
do_action('wp_custom_login_header_after');
}
add_action( 'login_footer', 'wp_custom_login_footer' );
function wp_custom_login_footer() {
do_action('wp_custom_login_footer_before');
get_footer();
do_action('wp_custom_login_footer_after');
}
function wp_custom_login_is_login_page() {
return in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'));
}
By saying you use the wp_login_form() function, I'm guessing you use a custom form on its own page.
There are a couple of things you can do.
If this form is on it's own page and you just want the incorrect password/username to go back to that page and alert the user, you can create a function in your functions.php file which will do a redirect to the referring page like so:
add_action( 'wp_login_failed', 'my_front_end_login_fail' ); // hook failed login
function my_front_end_login_fail( $username ) {
$referrer = $_SERVER['HTTP_REFERER']; // where did the post submission come from?
// if there's a valid referrer, and it's not the default log-in screen
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
wp_redirect( $referrer . '?login=failed' ); // let's append some information (login=failed) to the URL for the theme to use
exit;
}
}
If you just want to remove the shake, you can use this piece of code in your functions.php file which will stop the shake.
function my_login_head() {
remove_action('login_head', 'wp_shake_js', 12);
}
add_action('login_head', 'my_login_head');
This last suggestion is just based on a user accessing the wp-login.php page and then kicking them back to your custom login page.
function redirect_login_page() {
$login_page = home_url( '/login/' ); // CHANGE THIS TO YOUR PAGE URL
$page_viewed = basename($_SERVER['REQUEST_URI']);
if( $page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
wp_redirect($login_page);
exit;
}
}
add_action('init','redirect_login_page');

Redirect to specific page if not logged in WordPress

I am trying to have it on my WordPress site to redirect to a landing page if the user is not logged in, but it always gets stuck in a redirect loop. This is basically what I have (in functions.php)...
add_action( 'template_redirect', function() {
if ( !is_user_logged_in() && ! is_page('login-to-view') ){
wp_redirect( site_url( '/login-to-view' ) );
exit();
}
});
I know I am late for the party. I heard Big and Pac were rocking hard...
Anyways, this is what I usually do:
Create a login page, e.g. www.site.com/login
In my functions.php I add the following code:
add_action( 'template_redirect', function() {
if( ( !is_page('login') ) ) {
if (!is_user_logged_in() ) {
wp_redirect( site_url( '/login' ) ); // redirect all...
exit();
}
}
});
Explanation:
What happens above is access to every page on the site is restricted except for the login page. You can choose to exclude or include additional pages if you want and also the wp-admin URLs as well.
The issue is that I had the function in functions.php. is_page didn't know what page it is on yet. I moved it to the header, just before the doctype declaration and it works now with just this...
if ( ! is_page('login-to-view') && ! is_user_logged_in() ){
wp_redirect( site_url( '/login-to-view' ) );
exit();
}
Another thing to note... I wasn't able to get it to work on my localhost, I'm thinking because site_url can't be found, and it was just redirecting to localhost:8888/login-to-view
I'm using MAMP, but I'm guessing you can set up URL paths like that in the PRO version.
Let's say the user is not logged in.
Then your code thinks in this way:
Is user logged in?
NO? Then do wp_redirect.
The user gets redirected to your page, and the process begins again. That's why you get the infinite loop.
Try altering the order of checks instead:
<?php
add_action( 'template_redirect', function() {
if ( ! is_page('login-to-view') && ! is_user_logged_in() ){
wp_redirect( site_url( '/login-to-view' ) );
exit();
}
});

Redirect to front page wordpress if not logged in

I'm looking to redirect a user to a specific page if they are not logged in.
So blog.com/welcome
So they can either be viewing this page or wordpress login pages.
I was trying to modify this plugin
http://wordpress.org/plugins/redirect-to-login-if-not-logged-in/
add_action( 'wp', 'dmk_not_loggedin_redirect' );
function dmk_not_loggedin_redirect() {
global $pagenow;
However I am ending up with redirect loops. And I could use some help to stop this.
if ( ! is_user_logged_in() && $pagenow != 'wp-login.php' or 'http://www.blog.co.uk/front' )
wp_redirect( 'http://www.blog.co.uk/front' , 302 ); exit; }
I tried with the action hook init but the current post/page information is not available yet. But then, template_redirect does the job and it only acts on the front-end, login and admin pages work normally.
Here, there's a page with the slug welcome, to check for a post use is_single('post-slug'):
add_action( 'template_redirect', 'redirect_so_18688269' );
function redirect_so_18688269()
{
if( !is_user_logged_in() && !is_page( 'welcome' ) )
{
wp_redirect( site_url( 'welcome' ) );
exit();
}
}

Redirecting Wordpress's Login/Register page to a custom login/registration page

I have a website, with a user system. I want to integrate wordpress's user system into that website's, but I still want to use the website's register/login pages. I don't want anybody to be able to login or register using Wordpress's login or registration forms. Instead, when they're try to access the login/registration pages in Wordpress, I want those pages to redirect them to my own login/registration pages.
Is there any way of doing this? I've tried Google, but all I could find was redirection AFTER the user logs in or registers, which is not what I want.
Thank you in advance.
For this you need to redirect login/registration page to your custom pages.
So, Write this code in your functions.php file Under your activated theme folder. Pass your custom page path as a Argument.
add_action('init','possibly_redirect');
function possibly_redirect(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
wp_redirect('http://google.com/');
exit();
}
}
To restrict direct access only for 'wp-login.php', without POST or GET request (useful for custom ajax login forms), I use the advanced function:
function possibly_redirect(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
if ( isset( $_POST['wp-submit'] ) || // in case of LOGIN
( isset($_GET['action']) && $_GET['action']=='logout') || // in case of LOGOUT
( isset($_GET['checkemail']) && $_GET['checkemail']=='confirm') || // in case of LOST PASSWORD
( isset($_GET['checkemail']) && $_GET['checkemail']=='registered') ) return; // in case of REGISTER
else wp_redirect( home_url() ); // or wp_redirect(home_url('/login'));
exit();
}
}
add_action('init','possibly_redirect');
The correct action hook is login_init that only fires on wp-login.php.
Here, no ?action=action-name is being requested, so it's the main login page:
add_action('login_init', function(){
if( !isset( $_GET['action'] ) ) {
wp_redirect( 'http://example.com' );
}
});
For all requests, we can use a specific hook login_form_ACTION-NAME, ie, postpass, logout, lostpassword, retrievepassword, resetpass, register and login. Example:
add_action('login_form_register', function(){
wp_redirect( site_url('custom-registration/') );
});
If you're making use of a custom login page, but still using wp_login_form(), be aware that the form will POST to wp-login.php, so you will want to check if $_POST is empty before redirecting.
function prefix_wp_login_redirect() {
global $pagenow;
if( $pagenow == 'wp-login.php' && empty($_POST)) {
auth_redirect();
exit();
}
}
You might be able to latch onto the login_head hook and issue a redirect there.

Categories