Part of the URL disappears with a Query Vars in WordPress

3

On my site I am looking for a search-type URL and listing contents of a CPT, but part of the URL in the browser disappears, example:

If I access the browser link in the same browser, check the content but disable ? s = wordpress of the URL in the browser, the same if I try to check the files of a CPT: link disappears ? post_type = custom_post_type , this can be confusing for a user.

I have disabled all the plugins, refreshed permalinks and many tests but I still do not achieve it, it also has nothing to do with my browser since I can see the complete URL of another site for example: link .

Install a plugin called Rewrite analyzer to look in specific what happens with the? s = wordpress and throw me the following:

Disable friendly URL's - reset the htaccess (insert the WP Basic Htaccess) and nothing.

I just found something very curious in the installation: I edited it in General Options: WordPress Address (URL) / Site Address (URL) with the URL ending in / but it always refreshes and leaves it without /.

Other information: The following URL can be seen well in the browser: link does not disappear? action = lostpassword.

    
asked by pio 19.08.2017 в 20:28
source

2 answers

1

Everything points to the redirection to canonical URLs ( template_redirect / redirect_canonical ) is modifying your GET request to make it point to the canonical.

It works by allowing a link of type https://pruebas/2017/08/1/hola-mudo/ to link to the correct https://pruebas/2017/08/1/hola-mundo/ when the title and the permanent link have been edited to be consistent, but someone has already linked to the incorrect one.

It is enabled by default in Wordpress and can sometimes generate that effect (although I have not managed to reproduce it unless you deactivate the permanent URLs).

Previous versions of Wordpress 4.4 used the action template_redirect , so remove_action('template_redirect', 'redirect_canonical') is used % to disable functionality.

A filter is used for this from 4.4, so use remove_filter instead .

In the functions.php of your theme you can include the following lines:

/* A partir de Wordpress 4.4 hay que usar el filtro en vez de la acción */
if (strcmp($wp_version, '4.4') >= 0) {
  remove_filter('template_redirect', 'redirect_canonical');
} else {
  remove_action('template_redirect', 'redirect_canonical');
}

If you want to make it persistent throughout all the themes you can create a very basic plugin and save it in wp-contents/plugins :

<?php
/**
 * Desactivar redirección de URLs canónicas
 *
 * @wordpress-plugin
 * Plugin Name:       NoMasRedireccionesCanonicas
 * Description:       Desactiva la redirección a URLs canónicas
 * Version:           1.0.0
 * Author:            Oscar Garcia
 * Author URI:        https://es.stackoverflow.com/users/18608/oscargarcia
 */

/* A partir de Wordpress 4.4 hay que usar el filtro en vez de la acción */
if (strcmp($wp_version, '4.4') >= 0) {
  remove_filter('template_redirect', 'redirect_canonical');
} else {
  remove_action('template_redirect', 'redirect_canonical');
}

I use $wp_version to find the Wordpress version and use the appropriate solution, but if you use an updated version ( 4.8.1 today) just use the remove_filter line only.

I created the file wp-contents/plugins/nomasredirect/plugin.php in my tests and after creating the directory and file you must activate it in the plugins administration panel:

PS: I just did the test in a Wordpress that I have and any of the two remove_filter or remove_action fulfill their mission. Have you checked the content of functions.php of your theme to see if there is any hook that could be causing that redirect?.

    
answered by 24.08.2017 в 08:53
1

In what it does to rewrite rules, you have on the one hand what is external to wordpress (although wordpress also manipulates it) which is what you have defined in the .htaccess.

On the other hand you have Wordpress also internally manages rewrite rules.

When you create a CPT, rules are created automatically, for example, so you can access it by slug.

In addition to the rules that are created by default, you can add additional rules when you define the CPT. I do not know if you did that or not.

To solve the problem you have, it is necessary to watch the whole movie and it takes a lot of time, trial and error.

What I can give you are some tools:

1) For the .htaccess, you can activate the debug level of how the mod_rewrite is resolved. See as in this question This is displayed in the web server log.

2) For the rewrite rules of wordpress. Check the api from wp_rewrites as a starting point. In particular, the global variable wp_rewrite has all the rules that are in progress.

You'll probably have to hook a hook when parsea the query or when you finished parsear.

3) Something that is useful to me to debug in wordpress is to put a small homemade plugin that writes to the wordpress log ( ../wp-content/debug.log ) and from the code log the output doing write_log( print_r($variable,true));

Note: For production you must remove the home plugin and the write_log () that are in the code. And put all the definitions below in false.

wp-config:

...

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

...

plugin:

<?php
/*
 Plugin Name: Logger
*/

if ( ! function_exists('write_log')) {
    function write_log ( $log )  {
        if (defined('WP_DEBUG_LOG') && true === WP_DEBUG_LOG){
            if ( is_array( $log ) || is_object( $log ) ) {
                error_log( print_r( $log, true ) );
            } else {
                error_log( $log );
            }
        }
    }
}

EDIT I

These two functions with their hooks in the functions.php helped me with a project I worked on recently.

You can see how to solve the query and which rewrite rule is being applied internally in Wordpress.

/******* Para Debugging  **********/

 function print_rewrite_rules($a){
    global $wp_rewrite;

    write_log('wp_rewrite rules:');
    write_log($wp_rewrite);

    write_log('********');
    write_log('Query Vars:');
    write_log($a);

    return $a;
}

add_filter('parse_request', 'print_rewrite_rules', 90, 1);


function print_parsed_request(&$args){
    write_log('Parsed Request:');
    write_log($args);
}

add_action('parse_request','print_parsed_request');
    
answered by 26.08.2017 в 16:43