I've created a function redirecting all my logged-out users to the membership page.
I want them to buy a package plan before they can view the page they tried to visit before being redirected.
If logged-in -> view the download page.
If logged-out -> redirect to the membership page -> purchase a package -> view the download page.
I want to add content to the membership page, something like:
'The product that you are trying to download cannot be purchased individually, but it's part of our membership package'
The problem is that I couldn't find a way to execute any function after the wp_redirect() function. So I don't know how to display the content only to those redirected.
Is there a way to achieve it?
in your header.php:
if( !is_user_logged_in() ){
nocache_headers();
wp_safe_redirect( get_permalink( 'YOUR_MEMBERSHIP_PAGE_ID' ) . '?message=true' );
exit;
}
in your functions.php:
function add_query_vars_filter( $vars ){
$vars[] = "message";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
in your membership page:
if( get_query_var('message') && !is_user_logged_in() ){
echo 'The product that you are trying to download cannot be purchased individually, but it\'s part of our membership package';
}
Related
I want to give access Woo product single page only for Logged in users.
I have a product listing on the Homepage and Shop page, What I want is: when a logged out user click on a Product, the user will get a Login form and after Logged in user will be redirected to the Product page that he wanted to View.
So the flow will be something like:
Home/Shop-> click to Product X -> Login page -> Redirect to Product X single page
Currently, I am using the regular Woo Login form created by this function
woocommerce_login_form()
I am trying bellow code snippets:
add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
wp_safe_redirect($location);
exit();
}
}
add_action('init','my_login_redirect');
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
var_dump($location);
//wp_safe_redirect($location);
}
-----------------------
AND ALSO THIS ONE
-----------------------
function redirect_after_login(){
global $wp;
$protocol='http';
if (isset($_SERVER['HTTPS']))
if (strtoupper($_SERVER['HTTPS'])=='ON')
$protocol='https';
if (!is_user_logged_in() && is_product() ){
$redirect = site_url() . "/my-account.php?redirect_to= $protocol://" .
$_SERVER["HTTP_HOST"] . urlencode($_SERVER["REQUEST_URI"]);
wp_redirect( $redirect );
exit;
}
}
add_action( 'wp', 'redirect_after_login', 3 );
In both of cases, the problem is, it always find the Login page as HTTP_REFERER / REQUEST_URI
Because currently, I am using below code to redirect Non-logged-in user who is trying to see the product page to the Login page:
add_action('template_redirect', 'ethis_redirect_for_loggedin_users');
function ethis_redirect_for_loggedin_users() {
if ( !is_user_logged_in() && is_product() ) {
wp_redirect(site_url().'/default-login');
exit;
}
}
You can use the template_redirect filter hook to prevent guest users to access a single product page.
add_action( 'template_redirect', 'wc_redirect_non_logged_to_login_access');
function wc_redirect_non_logged_to_login_access() {
if ( !is_user_logged_in() && is_singular( 'product' ) ) {
global $post;
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id')).'?redirect='.get_the_permalink( $post->ID ) );
exit();
}
}
Then after you have to use the woocommerce_login_redirect filter hook for login redirect.
add_filter( 'woocommerce_login_redirect', 'my_login_redirect', 10, 2 );
function my_login_redirect( $redirect, $user ) {
if( isset( $_GET['redirect'] ) && $_GET['redirect'] != '' ){
return $_GET['redirect'];
}
return $redirect;
}
Code will go in your active theme functions.php Tested and Works.
I would like to redirect the user that is logging out to the same page.
For instance: user is on category page and is logging out. After logging out I would like to have that the user is still on that specific category page (or any page the user is on). Now it will redirect the user to the homepage.
This is the code I'm using:
function iconic_bypass_logout_confirmation() {
global $wp;
if ( isset( $wp->query_vars['customer-logout'] ) ) {
wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'home' ) ) ) );
exit;
}
}
add_action( 'template_redirect', 'iconic_bypass_logout_confirmation' );
I tried to change line 5 with the code below but without result.
wp_redirect( str_replace( '&', '&', wp_logout_url( get_permalink() ) ) );
According to the docs on wp_logout_url(), you can specify the redirect url. In this case, you should update the Logout link itself. So if you had a logout link in the header.php file as an example, you could update the wp_logout_url() reference to include the current page url. So, anytime a user clicked the logout link, it would also have the current page as the redirect link.
All you need is the current page url. Referencing this post you can see one method for getting the current page url. This combined with the wp_logout_url() function, and your HTML might look something like this,
<?php global $wp; ?>
Logout
Now, the user will always be redirected from the previous page. If you do not have control over the Logout link itself, your only options would be to handle the logout via AJAX, or track the previous url yourself, via a query variable, session, cookie, etc.
You can try this and set your own redirect url
<?php
//wordpress logout
add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
function logout_without_confirm($action, $result)
{
/**
* Allow logout without confirmation
*/
if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] :
"https://example.com";
$location = str_replace('&', '&', wp_logout_url($redirect_to));
header("Location: $location");
die;
}
}
I'm sure this was working but now it's not. I added categories to pages with:
function add_categories_to_pages() {
register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_categories_to_pages' );
then tagged a range of pages with a 'client' category and added to functions:
add_action( 'template_redirect', 'client_redirect_to_login' );
function client_redirect_to_login() {
$category_slug = 'client';
global $pages;
if ( ! is_user_logged_in() && in_category( $category_slug, $pages ) ) {
wp_redirect( site_url( '/login' ) );
exit();
}
}
the intention, simply enough, is that not logged in users trying to directly access any of the 'client' pages is redirected to my custom /login page. being returned directly to the page they were trying to access would be a bonus but this, THIS, used to work. there's a dozen pages and growing, so restricting by category is easier than by page ID array - but i can't see what i'm doing wrong.
all advice greatly appreciated!
From global variables pages in codex $pages will return:
The content of the pages of the current post. Each page elements contains part of the content separated by the <!--nextpage--> tag.
And you need either post/page ID or object when using in_category() function as a second argument, as noted here.
You can try to get the category of the visited page by setting:
$current_cat = get_the_category();
And then check
in_category( $category_slug, $current_cat );
inside the conditional.
I have a website of sports competitions. I can create the calendars of future matches, and then I set the post status to "private".
Users that are NOT logged in can click on the post and access it.
How can I disable the link to the post for users not logged in?
Put this code inside your function.php file.
function getPrivatePostMatch()
{
global $post;
$status = get_post_status( $post->ID );
if( $status == "private" )
{
wp_redirect( home_url() ); // you can add your page url here
exit;
}
}
add_action('template_redirect','getPrivatePostMatch');
I'm trying to create a WordPress site with a login system, but I only want the login to be required to view POSTS. They should be able to view pages without logging in. I did some research, but no one had exactly what I was looking for, so I tried to puzzle something together, and this is what I came up with, but it doesn't work.
function my_force_login() {
global $post;
if ( ( is_single() && !is_user_logged_in() ) ){
auth_redirect();
}
}
Any ideas?
add it to your single.php
<?php auth_redirect(); ?>
This function will check if the user is logged in and if he is not it will redirect him to the login page http://codex.wordpress.org/Function_Reference/auth_redirect
Your code works, but it has run on the correct point:
add_action( 'template_redirect', function()
{
global $post; // Use this to detect the custom post types if needed
if ( ( is_single() && !is_user_logged_in() ) )
{
auth_redirect();
}
});
Details of the post is being displayed in the single.php. Please put the code of displaying post's content into the condition. Then you will get the solution. Try the following code into the single.php in the twentyforteen theme.
if ( is_user_logged_in() ) {
get_template_part( 'content', 'single' );
}
Now get logged out and refresh the browser, you can see none of content are shown.