Woocommerce custom logout without confirmation - wordpress

I am trying to implement custom logout without logout confirmation in woocommerce.
I created a page and added this code. and added link of this page to menu. But its not working.
session_start();
session_destroy();
header("Location:http://www.liberatium.com/");

Confirmation happens because you are missing the neccessary nonce in the URL, which is being checked in wp-login.php
case 'logout' :
check_admin_referer('log-out');
...
Use wp_logout_url in order to retreive the URL including the nonce. If you want to redirect to a custom URL, simply pass it as an argument.
Log out
if that's not working means, Add below function in functions.php and try above code...
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'] : '';
$location = str_replace('&', '&', wp_logout_url($redirect_to));;
header("Location: $location");
die();
}
}

I use the woocommerce endpoing logout in the menu item with link like
https://yoursite/my-account/customer-logout/?_wpnonce=2bbbac43a8&customer-logout=true
&customer-logout=true - is keypoint here to logout without confirmation. Just add it to the and of link in menu item.
One disadvantage of this method - after successfull logout user is redirecting to login page, not to the current page.

I used this code in the wordpress functions.php, to logout user after payment or close the browser
/**
* Bypass logout confirmation.
*/
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( 'myaccount' ) ) ) );
exit;
}
}
add_action( 'template_redirect', 'iconic_bypass_logout_confirmation' );

You can create new shortcode log out URL in function.php of theme and add everywhere you want in tag html. I tried and success.
// Custom shortcode log out
function custom_logout()
{
return wp_logout_url(bloginfo( 'url' ));
}
add_shortcode( 'custom_logout_s', 'custom_logout' );

You can use this if you prefer a logout shortcode tag for html (with optional redirect slug between tags):
/* Shortcode for no confirmation logout link with optional redirect slug between shortcode tags */
//
// E.g. [logout_link]my-account[/logout_link]
//
function logout_to_optional_redirect_slug_function( $atts, $redirect_slug = null ) {
$redirect_full_url = get_site_url(null, '/', 'https') . $redirect_slug;
$logout_url = wp_logout_url($redirect_full_url);
$logout_hyperlink = "<a href='".$logout_url."'>Logout</a>";
return do_shortcode($logout_hyperlink);
}
add_shortcode( 'logout_link', 'logout_to_optional_redirect_slug_function' );

Related

I want to skip a specific form of WP Forms, if that is already submitted by a user

I have an opt-in form which I wanted to show when a visitor came to that page until visitor submits the form. Once visitor submitted that opt-in form a specific page then that page should not appear, instead redirect to another page.
you can achieve this by using process complete hook of wp forms.
function wpform_set_submitted_cookie( $fields, $entry, $form_data, $entry_id ) {
// Set the third parameter to specify a cookie expiration time,
// otherwise it will last until the end of the current session.
setcookie( 'wpform_form_submitted', 'true' );
}
add_action('wpforms_process_complete_{form-ID}',
'wpform_set_submitted_cookie',10,4);
function wpform_protect_confirmation_page() {
if(is_page('{your-page-slug}')&&isset( $_COOKIE['wpform_form_submitted'])) {
wp_redirect( home_url( '/' ) );
exit();
}
}
add_action( 'template_redirect', 'wpform_protect_confirmation_page' );

wc_logout_url without confirmation AND redirect to current page in Wordpress WooCommerce

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

How to redirect a user not logged in to a different page in wordpress

I would like to redirect a user that is not logged in to the page I created to log in. I wrote the code in the functions.php of the theme but it seems like that it doesn't working.
<?php
if (is_page($page = 'private-gallery' )) {
if (!is_user_logged_in() ) {
template_redirect ('http://www.site.it/wp/private-area' );
exit;
}
}
?>
You can do this by hooking into the template_redirect [action reference]. (note that you can also use an earlier executed hook, but I mostly use this for ease).
This way you can retrieve the current post slug, and based on this do your statement comparison to see if the current page is private-gallery.
In the below method the wp_redirect() [function reference] is used to redirect to the correct page. The home_url() [function reference] is used to retrieve the home URL for the current site (always try to avoid use full path URL's whenever possible).
The wp_redirect basically executes header("Location: $location", true, $status); where $status is 302 by default .
add_action('template_redirect', 'gianni_private_gallery_redirect');
function gianni_private_gallery_redirect(){
global $post;
if($post){
if( $post->post_name=='private-gallery' ) {
if ( !is_user_logged_in() ) {
// By default a 302 redirect.
// Add your custom status code as second parameter if required.
wp_redirect( home_url( '/private-area/' ) );
exit;
}
}
}
}

Wordpress disable private posts links

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

wordpress - Change page name when user is logged out

I have a page named My account, I want when I log out to change the title of the page in "Log In / Register".
I tryed this code:
function wp_change_title( $title ) {
if ( !is_user_logged_in() && get_page_by_title( 'My Account' ) ) {
return 'Log In / Register';
}
return $title;
}
add_filter( 'the_title', 'wp_change_title' );
But it changes the name to all the pages when I log out, how do I do to only change the name of the My account page?
try is_page('My Account') instead of get_page_by_title(). get_page_by_title() is a query, not a conditional.

Resources