I'm making an advertisement service, I'm using wordpress template. It works fine, but from a week I realise that I've a problem, I don't know when I destroy something.
When I'll post new ad the page is checking if am I logged in.
if ( !is_user_logged_in() ) {
$login = $redux_demo['login'];
wp_redirect( $login ); exit;
} else {
}
And mostly it redirect me into Login page (I'm logged in).
On the Login page it checks:
if ( is_user_logged_in() ) {
global $redux_demo;
$profile = $redux_demo['profile'];
wp_redirect( $profile ); exit;
}
And it is redirecting me into Profile page! So in the first time it return that I'm not logged in, but on the second page it return that I'm logged in.
Sometimes it works. For example when I login and wait a few minutes it works correctly, but when I sign out and login there's the same problem. Do you have some ideas how to fix it?
The best practice for redirecting is to use the WordPress action event hook called template_redirect. This event ensures that you are:
Checking for the user's login state after that information is available (i.e. and not too early in the load process)
WordPress is ready to accept a redirect
In your use case, you would want to do the following:
add_action('template_redirect', 'redirect_to_login_page_if_not_logged_in');
/**
* Redirect to the login page if the user is not logged in.
*
* #since 1.0.0
*
* #return void
*/
function redirect_to_login_page_if_not_logged_in() {
if ( is_user_logged_in() ) {
return;
}
global $redux_demo;
if ( isset( $redux_demo['login'] ) ) {
wp_redirect( esc_url( $redux_demo['login'] ) );
exit();
}
// Redirect to the built-in WordPress login page
auth_redirect();
}
If the user is logged in, just bail out (with the return). Else, check if the 'login' key is set in your global variable. If yes, redirect to that page. Else, redirect to the built-in WordPress login page using auth_redirect().
Related
I'm using WordPress and I want the user to go to the Home Page on their first visit, but on every other visit after that I would like them to be redirected to the Blog.
Home Page:
www.website.com
Blog:
www.website.com/blog
I'm guessing the best way to do this is to set a cookie?
I have no idea on what PHP files to edit or anything...
In your theme functions.php ( or plugin )
function o99_set_newvisitor_cookie() {
if ( !is_admin() && !isset($_COOKIE['sitename_newvisitor'])) {
setcookie('sitename_newvisitor', 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);
}
}
add_action( 'init', 'o99_set_newvisitor_cookie');
After that
if (isset($_COOKIE['sitename_newvisitor'])) {
echo 'Welcome back!'; // or redirect using wp_redirect( 'some_url/' ); exit;
}
else {
echo 'Hello new visitor!'; // or redirect using wp_redirect( home_url() ); exit;
}
This should do the job .
Wordpress itself had a function called wp_setcookie() but it was deprecated and replaced by wp_set_auth_cookie() which is only for user auth I believe . Not sure why, but maybe because of cookies laws that were introduced ( and that also you need to take into account )
Anyhow, see also the normal PHP setcookie() docs and the wp_redierct() function in codex.
I have a page that is restricted to logged in users (say it's called '/authed-page'). When a non-authenticated user visits this page they are prompted to click a button to login and they are then taken to the login url: '/login'
Now after they login I want them to get redirected to the page they had been trying to visit, '/authed-url', but I can't figure out how to do this!
Things I've tried:
1) Declaring a function redirect_to_authed_url in my functions.php file like this
function redirect_to_authed_url( $redirect_to, $request, $user ) {
return "/post-a-job";
}
Then in the template for authed-url.php when I detect the user isn't logged in I do this:
<?php add_filter( 'login_redirect', 'redirect_to_authed_url', 10, 3 ); ?>
However this doesn't work I think because the user presses a button which generates another http request which sends them to the '/login' url which means the redirect filter is lost.
2) Setting a cookie when someone visits '/authed-url' and they are not logged in called 'redirect_url'. Then I declare this in my theme's functions.php file
function redirect_request( $redirect_to, $request, $user ) {
if (isset($_COOKIE['login_redirect']) && strpos($_COOKIE['login_redirect'], 'http') === false) {
$redirect_url = $_COOKIE['login_redirect'];
unset $_COOKIE['login_redirect'];
return $redirect_url;
} else {
return('/');
}
}
This doesn't seem to work because I can't figure out where to set the cookie. I keep getting the warning:
WARNING: CANNOT MODIFY HEADER INFORMATION - HEADERS ALREADY SENT
I feel like I must be missing something obvious. This should be a pretty common requirement no? Any ideas?
You are able to append a URL encoded redirect_to variable to the login URL to get it to redirect after login.
For example:
http://www.example.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.example.com%2Fauthed-url
That URL will redirect you to /authed-url after login.
you can try this function to check user logging or not.
<?php
if( is_user_logged_in() ) {
echo 'your function for logging user';
}else{
echo 'your function for not logging user';
}
?>
I render login form on my header template using this code:
<?php wp_login_form($args); ?>
When I pass proper credentials, it redirect me to homapage and all seems to be fine, but when I put wrong login or pass, it redirect me to the folowing url:
http://localhost/wordpress/wp-login.php
So the question is how I can output errors on the same page , and prevent redirection to the wp-login ? I try to find solution but didnt have any results. Thanks!
Add this to your functions.php :
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;
}
}
This code redirects to the same page as the user tries to log in from.
Change $referrer for another page.
Hope it will works for you.
I want to make my wordpress site hidden or restricted from guest user. Only the the registered member will see my pages, if they are not registered then they will be redirected in login page/ registration page.
When i am tried to use some plugin to do that then all of my pages restricted and there had option to exclude only one page (login page). But i guest cant register there. Because all of the page restricted excluding loging page. SO thats a problem.
Now i think i can use custom code something like that
<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';} else { echo 'You have no rights to access this page. Please log in or a href=\"#\">register</a> now to access that page';
}
?>
But how can i set a function , "If logged in then full page will open other wise show a text"
If you want to hide the content, then you can use the_content hook to change the page content for not loggedin user.
add_filter('the_content', 'restricted_content');
function restricted_content($content)
{
if( !is_user_logged_in()
{
$content = 'You have no rights to access this page...blah blah..';
}
return $content;
}
Now, if the login/registration is a General WordPress Page created using WP Admin, you can either exclude it by id, name or slug.
function restricted_content($content)
{
if( !is_user_logged_in() && ! is_page('login') && ! is_page('registration') // an id or name can be passed also
{
$content = 'You have no rights to access this page...blah blah..';
}
return $content;
}
I am fairly new to WordPress. On my homepage I have a navigation bar which I only want to show to people who are logged in as users.
In my header.php the function is_logged_in doesn't seem to work.
I want to place a condition in my header.php file to check if the user has logged in (and then display the navigation).
Any advice would be helpful.
Use the is_user_logged_in function:
if ( is_user_logged_in() ) {
// your code for logged in user
} else {
// your code for logged out user
}
Example: Display different output depending on whether the user is logged in or not.
<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
echo 'Welcome, visitor!';
}
?>
Try following code that worked fine for me
global $current_user;
get_currentuserinfo();
Then, use following code to check whether user has logged in or not.
if ($current_user->ID == '') {
//show nothing to user
}
else {
//write code to show menu here
}
get_current_user_id() will return the current user id (an integer), or will return 0 if the user is not logged in.
if (get_current_user_id()) {
// display navbar here
}
More details here get_current_user_id().
This problem is from the lazy update data request of Chrome.
At the first time you go to homepage. Chrome request with empty data. Then you go to the login page and logged in. When you back home page Chrome lazy to update the cookie data request because this domain is the same with the first time you access.
Solution:
Add parameter for home url. That helps Chrome realizes that this request need to update cookie to call to the server.
add at dashboard page
<?php
$track = '?track='.uniqid();
?>
<img src="/img/logo.svg">
I think that.
When guest is launching page, but Admin is not logged in we don`t show something, for example the Chat.
add_action('init', 'chat_status');
function chat_status(){
if( get_option('admin_logged') === 1) { echo "<style>.chat{display:block;}</style>";}
else { echo "<style>.chat{display:none;}</style>";}
}
add_action('wp_login', function(){
if( wp_get_current_user()->roles[0] == 'administrator' ) update_option('admin_logged', 1);
});
add_action('wp_logout', function(){
if( wp_get_current_user()->roles[0] == 'administrator' ) update_option('admin_logged', 0);
});