Minimalize URL redirections (search request) - php

On my wordpress site I have changed my search slug from ?s=some-word into /search/some-word with this part of code:
function change_search_slug() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'change_search_slug' );
Everything is working fine, but I get a redirection 302 from my-site.com?s=some-word into my-site.com/search/some-word. I am wondering if there is a way to make only one request without the redirection.
Thanks in advance for help :)

Related

How to log out of WooCommerce without confirming?

We want the users of a website to close the session without having to confirm it.
Currently, when we press the "Log Out" button, the user is directed to the URL htt://myWebSite.com/my-account/customer-logout/ and we must press "Confirm and Exit"
Well, we want to avoid this step and automatically log out when the user clicks the Logout Button
There is some plugin that could do this, but we prefer to use functions. We've tried things, but we can't get it right to get it done
add_action('wp_logout','ps_redirect_after_logout');
function ps_redirect_after_logout(){
wp_redirect( 'https://miPaginaFavorita.com' );
exit();
}
Another option that we have handled is the following, but it does not work either
add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
function logout_without_confirm($action, $result)
{
/**
* Allow log out without confirmation
*/
if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
$redirect_to = isset($_REQUEST['redirect_to']) ?
function logout_without_confirm($action, $result)
{
/**
* Allow log out 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();
}}
How can I modify my function to avoid confirming when logging out?
Thanks
you most change url function in your button . check your theme file how create button for logout .
use 'wp_logout_url' function for skip Confirm page .
in my theme like this :
if ( class_exists( 'WooCommerce' ) ) {
$logout_link = wc_get_endpoint_url( 'customer-logout', '', wc_get_page_permalink( 'myaccount' ) );
}
$logout_btn = '<a class="login" href="' . esc_url( $logout_link ) . '">Logout</a>';
For example, I did this to skip the page confirmation and work fine :
$logout_link = wp_logout_url( get_home_url() );
$logout_btn = '<a class="login" href="' . esc_url( $logout_link ) . '">Logout</a>';
I hope it helped you.
Edit --
METHOD 2 :
simple way => if you are redirect to "/customer-logout" when click logout button you can check and logout user with this function .
add code in your functions.php (is in your theme folder or child theme folder) .
function skip_logout_confirmation() {
global $wp;
if ( isset( $wp->query_vars['customer-logout'] ) ) {
wp_redirect( str_replace( '&', '&', wp_logout_url( home_url() ) ) );
exit;
}
}
add_action( 'template_redirect', 'skip_logout_confirmation' );

redirect to specific page after login based on browser language

in my functions.php i have a redirect after login. currently this:
add_action( 'template_redirect', 'wpdm_login_redirect' );
function wpdm_login_redirect(){
if( is_user_logged_in() && get_the_ID() == get_option('__wpdm_login_url') ):
wp_redirect( home_url('/dashboard') );
exit();
endif;
}
this works as expected, but i need to combine it with a redirect to a different page if the browser language is detected as italian, so an if ($lang=="it_IT") redirect to home_url('/area_personale'), and everyone else to '/dashboard'
i'm very new at this, so my problem is how to get this language stuff into my existing login redirect. or maybe there is an 'easier' way to go about this? any guidance most appreciated!
This should do the trick.
add_action( 'template_redirect', 'wpdm_login_redirect' );
function wpdm_login_redirect(){
$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if( is_user_logged_in() && get_the_ID() == get_option('__wpdm_login_url') && $language == "it_IT" ):
wp_redirect( home_url('/area_personale') );
exit();
else:
wp_redirect( home_url('/dashboard') );
endif;
}

WordPress: how to force a 404 when accessing wp-admin?

I have a custom log-in form on a custom page that handles all logging in/out. The form posts to wp-login.php as required.
<form name="loginform-custom" id="loginform-custom" action="http://localhost/wp-login.php" method="post">
All invalid log-in attempts are handled by this following script in the functions.php file:
/**
* Redirect user on invalid log-in attempts
*/
function login_failed() {
wp_redirect( home_url('/access-denied') );
exit;
}
add_action( 'wp_login_failed', 'login_failed' );
function verify_username_password( $user, $username, $password ) {
if( $username == "" || $password == "" ) {
wp_redirect( home_url('/access-denied') );
exit;
}
}
And I use the following script in my functions.php file to redirect all non-admins away from wp-admin to a 404 page.
function restrict_admin_with_redirect() {
if ( ! current_user_can( 'manage_options' ) && ( ! wp_doing_ajax() ) ) {
wp_redirect( site_url('/404') );
exit;
}
}
add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );
However, here is my problem. When non-users (random visitors) try to access wp-admin, they are taken to the access denied page despite the redirect script to 404. For some reason, whenever anybody tries to access wp-admin, the server treats it as a log-in attempt and when they are (of course) denied log-in, they are redirected to the access denied page.
Is there any way I can force a 404 when wp-admin is accessed by a user? Also, I'd like a true 404 and not a redirect to a 404 page (which is not a true 404).
Thoughts?
Is there a way to force anyone (users and non-users) to a 404 when they try to access wp-admin? In the action script above, even if I redirect non-admins to a 404.php page, non-users still get sent to the standard access d
Consider this solution from Ragu.cz.
https://wordpress.org/support/topic/make-wp-admin-throw-a-404-page-or-something/#post-8565864
add_action( 'init', 'force_404', 1 );
function force_404() {
$requested_uri = $_SERVER["REQUEST_URI"];
do_action('debugger_var_dump', $requested_uri, '$requested_uri', 0, 0);
do_action('debugger_var_dump', strpos( $requested_uri, '/wp-login.php'), 'FOUND?', 0, 0);
if ( strpos( $requested_uri, '/wp-login.php') !== false ) {
do_action('debugger_var_dump', 'REDIRECT', 'REDIRECT', 0, 0);
// The redirect codebase
status_header( 404 );
nocache_headers();
include( get_query_template( '404' ) );
die();
}
if ( strpos( $requested_uri, '/wp-login.php') !== false || strpos( $requested_uri, '/wp-register.php') !== false ) {
do_action('debugger_var_dump', 'REDIRECT', 'REDIRECT', 0, 0);
// The redirect codebase
status_header( 404 );
nocache_headers();
include( get_query_template( '404' ) );
die();
}
if ( strpos( $requested_uri, '/wp-admin') !== false && !is_super_admin() ) {
do_action('debugger_var_dump', 'REDIRECT', 'REDIRECT', 0, 0);
// The redirect codebase
status_header( 404 );
nocache_headers();
include( get_query_template( '404' ) );
die();
}
do_action('debugger_var_dump', 'END', 'END', 0, 0);
}

Use a $GET parameter from a URL, to create a new redirect URL (Wordpress)

I am using a custom login page in Wordpress, and I installed a script in the functions.php that redirect to a custom "error login" page (adding parameters to the login URL), so it can display the errors in the same page instead of the native login page.
But in some cases, this login page contains some parameters already, when someone is redirected there after trying to access a private page.
For example :
Regular case : They try to access : website.com/login. If there is an error when trying to login, my script redirect to website.com/login?login=empty, and they can try again.
Problematic case : They try to access website.com/protected-page/private, they are automatically redirected to website.com/login?members=%2Fprotected-page%2Fprivate%2. If there is an error when trying to login, my script actualy redirect to website.com/login?login=empty (same as regular case), and what I'm trying to achieve, is to redirect to website.com/login?members=%2Fprotected-page%2Fprivate%2&login=empty, when an error is made.
Keeping this "?members" parameter in the URL allow the form to redirect to the previous page they were trying to access before, after a successful login. Otherwise with the regular case, they are redirected to the general dashboard.
The script that actually manage the redirect when error is this one :
add_action( 'authenticate', 'check_username_password', 1, 3);
function check_username_password( $login, $username, $password ) {
$referrer = $_SERVER['HTTP_REFERER'];
if( !empty( $referrer ) && !strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) ) {
if( $username == "" || $password == "" ){
wp_redirect( get_permalink( 20 ) . "?login=empty" );
exit;
}
}
So the important part here, that will create the error URL is :
wp_redirect( get_permalink( ID ) . "?login=empty" );
I can get my other UTL parameter with "$_GET["members"]", if I do an "echo $_GET["wlfrom"];" it displays successfully the parameter in the page. But even if it looks so simple, I don't manage to add this parameter to the URL. What I'v tried so far :
#1. wp_redirect( get_permalink( ID ) . "?members=" . $_GET["members"] . "?login=empty" );
#2. wp_redirect( get_permalink( ID ) . "?members=" . echo $_GET["members"] . "?login=empty" );
#3. $param = array('members');
wp_redirect( get_permalink( ID ) . "?members=" . $param . "?login=empty" );
//This one returns website.com/?members=array/?login=empty
#4.$param = $_SERVER["PHP_SELF"];
wp_redirect( $param . "?login=empty" );
// This one I cannot use because it displays the native login URL, not my permalink, that's why I need to use "get_permalink" for the first part of the URL
And various other solutions too. I'm a newbie, so I can't find of other solutions to create this new URL based on the previous one, I'm not sure either if using GET is the way to go also.
What should I fill up "wp_redirect" with in order to use the parameter from the current URL ?
Thank you !
put a hidden field in your wordpress custom login form
<input type="hidden" name="members" value="<?php echo #$_REQUEST['members']; ?>">
and change these
wp_redirect( get_permalink( ID ) . "?login=empty" );
with
<?php
$queryarg='?login=empty';
if(isset($_REQUEST['members']) && $_REQUEST['members']!=''){
$queryarg .='&members='$_REQUEST['members'];
}
wp_redirect( get_permalink( ID ) . $queryarg );
?>

Wordpress admin stopped working after upgrade to 3.5.1

Today I tried to update Wordpress to the latest version (3.5.1). After doing this, I can't open wp-admin/index.php anymore. It gives me a 404 error. I've looked into the index.php file and it breaks when the function auth_redirect() is called. Here's the code of that function:
function auth_redirect() {
// Checks if a user is logged in, if not redirects them to the login page
$secure = ( is_ssl() || force_ssl_admin() );
$secure = apply_filters('secure_auth_redirect', $secure);
// If https is required and request is http, redirect
if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
exit();
} else {
wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
exit();
}
}
if ( is_user_admin() )
$scheme = 'logged_in';
else
$scheme = apply_filters( 'auth_redirect_scheme', '' );
if ( $user_id = wp_validate_auth_cookie( '', $scheme) ) {
do_action('auth_redirect', $user_id);
// If the user wants ssl but the session is not ssl, redirect.
if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
exit();
} else {
wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
exit();
}
}
return; // The cookie is good so we're done
}
// The cookie is no good so force login
nocache_headers();
$redirect = ( strpos( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) ? wp_get_referer() : set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
$login_url = wp_login_url($redirect, true);
wp_redirect($login_url);
exit();
}
However I can't find the specific part where it breaks, since it's not giving me an error message, it just shows a 404 page, and in Firefox it says it's not redirecting correctly.
Could someone please help me out on this?
Thank you!
Some additional information:
I've found the line where it breaks, it is:
wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
Echoing $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] gives me the expected result (www.domain.com/blog). However it just doesn't work :(
I have had similar issues in the past. But it usually involves a new plugin installation... Couple of things:
Try to increase the RAM available. WP has given me hassles in the past similar to this and it was RAM related. You should be able to increase the RAM in your .htaccess file
Is this a production system? If not, perhaps make a note of all the plugins, delete them, try access the system. If that works, then you know the issue was plugin based and not WP itself. You can then systematically add the plugins back one by one and determine which one caused the issue.

Categories