Conditional page for logged user (Member Only Page) - php

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;
}

Related

How to redirect Woocommerce to a specific page?

I use "Woocommerce User Email Verification" which after the user registers, it redirects user to the main page. It doesn't have built in option to redirect users to a page where I can tell them that they should check their email and confirm it to access my-account section!
Plugin link:
https://wordpress.org/plugins/woo-confirmation-email/
download link:
https://downloads.wordpress.org/plugin/woo-confirmation-email.3.1.15.zip
Please tell me how I can redirect a user after they press the Register to any page of my own website that I want?
Ps. I know that without this plugin, when user registers, they automatically go to my-account section. So this plugin is causing the Redirect I think.
Would you be so kind and help?
Thanks!
Try to add this in function.php
function wpse_registration_redirect() {
return home_url().'/my-page' ;
}
add_filter( 'registration_redirect', 'wpse_registration_redirect' );
or
function custom_registration_redirect() {
return home_url().'/my-page' ;
}
add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);
or
After login
function admin_default_page() {
return home_url().'/my-page' ;
}
add_filter('login_redirect', 'admin_default_page');
or After WooCommerce login
function iconic_login_redirect( $redirect_to) {
$redirect_to = 'http://yoursiteshop';
return $redirect_to ;
}
add_filter( 'woocommerce_login_redirect', 'iconic_login_redirect',1100, 2 );

is_user_logged_in conflict with wp_redirect

I am trying to hide my custom post type single page from non-logged in users. I used hook template_redirect as follows:
add_action('template_redirect','hide_single_property');
function hide_single_property()
{
if( is_singular('property') || is_page('dashboard')):
if( ! is_user_logged_in() ):
wp_redirect(get_permalink(103),302);
exit;
endif;
endif;
}
the code above works, but with some problem. Like i try to visit http://example.com/property/abc it redirects to login page. And after login if i try to visit the same post it again redirects back to login page, however works fine with other properties.
It just loads the url before login again :(
if its just the single post you're trying to hide, you can add the following to your sinlge-property.php template file:
if ( is_user_logged_in() ) {
//your content file
get_template_part('content');
}
else {
//show login page
get_template_part('must-login');
}

Wordpress check if user is logged in works wrong

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().

Redirect a user to the page they were trying to access after login in Wordpress

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';
}
?>

WordPress - Check if user is logged in

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);
});

Categories