Check if user is registered or not in wordpress and redirect URL after login

Recently we needed to make sure, the visitor to our website is shown a message asking “Login” and if the user is logged in hide this message, something like “Welcome Visitor! Login Please !” , so we used following code ( API’s),

<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
echo 'Welcome, visitor! <a href="http://ask.greenecosystem.in"> Login  Please</a>  ';
}
?>

We added this code to wp-content/themes/theme_name/header.php , so it can appear to all URLs where user visit, but soon we found that, we need to hide this message from home page and register url, so we modified code as below,

<?php
if ( is_user_logged_in() ) {
          echo 'Welcome, registered user!';
} else {
         if (!is_page('register') ) {
               echo 'Welcome, visitor! <a href="http://ask.greenecosystem.in"> Login  Please</a>  ';
         }
}
?>
where, register is a page, where we don't want the message to be seen by user.

Now comes the scenario, that if the user logs in, he should be redirected to the same URL, from he has tried to login, so he should not miss the URL of page which he visited before login, so the final code became as,

<?php
if ( !is_user_logged_in() ) {
$url = wp_login_url( get_permalink() );
if (!is_page('register')) {
echo 'Welcome Visitor! <a href="' . $url . '" title="Login">Login</a> Please ! ';
}
}
?>

Hope this will help 🙂

Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense Multimedia - Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Web design and development Wordpress Yocto / Bitbake / Openembedded
Tags:

Leave a Reply / Ask Question