Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirection after buffer starts adds the old url to the preload cache table #7174

Open
wordpressfan opened this issue Dec 9, 2024 · 2 comments
Labels
3rd party compatibility Issues related to 3rd party compatibility like theme, plugin or hosting

Comments

@wordpressfan
Copy link
Contributor

wordpressfan commented Dec 9, 2024

Describe the bug
We start our buffer with template_redirect action and close it with shutdown hook, so if we have a code (custom or in another plugin) that tries to redirect after template_redirect the page url will be saved in preload cache database table.

To Reproduce
Steps to reproduce the behavior:

  1. Use the following snippet:
add_action( 'plugins_loaded', function () {
	$current_url = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( home_url( $_SERVER['REQUEST_URI'] ) ) : '';
	if ( 'http://wprocket.localhost/fonts/' !== $current_url ) { // Replace this url with the page or post url u want.
		return;
	}
	wp_redirect( home_url( '/' ), 301 );
	exit;
}, 3 );
  1. Make sure u use this snippet inside a plugin (not inside the theme and not inside snippets plugin) and change the url to match with a real page/post url
  2. Make sure that the url is not in cache database table and it's not cached at all.
  3. Visit the mentioned url
  4. You will be redirected to home page.
  5. The url will be inside cache table and its status is completed.

Expected behavior
This url shouldn't be saved inside preload cache table.

Additional context
Try the following snippet instead, it'll work and fix the issue:

add_action( 'template_redirect', function () {
	$current_url = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( home_url( $_SERVER['REQUEST_URI'] ) ) : '';
	if ( 'http://wprocket.localhost/fonts/' !== $current_url ) {
		return;
	}
	wp_redirect( home_url( '/' ), 301 );
	exit;
}, 1 );

This happened with Translatepress - Personal

Slack discussion: https://wp-media.slack.com/archives/C056ZJMHG0P/p1733428826901719

Proposed solution for translatepress

The have this line:

$this->loader->add_action( 'plugins_loaded', $this->slug_manager, 'translate_request_uri', 3, 1 );

exactly here:

wp-content/plugins/translatepress-personal/add-ons-advanced/seo-pack/class-seo-pack.php:70

if they managed to change it to be

$this->loader->add_action( 'template_redirect', $this->slug_manager, 'translate_request_uri', 1, 1 );

I think this will fix the issue

@piotrbak piotrbak added the 3rd party compatibility Issues related to 3rd party compatibility like theme, plugin or hosting label Dec 9, 2024
@razvan-translatepress
Copy link

Hello,

I'm Razvan, developer at TranslatePress. Thank you for sharing a detailed report on this compatibility issue.

Moving the entire translate_request_uri function to the template_redirect hook is not possible because it handles identifying what original URL matches the currently loaded translated URL. This needs to be done earlier than template_redirect.

However, the redirect step can be moved to template_redirect hook. This should solve the issue reported and keep TranslatePress functionality working as expected. The fix should be present in the next update of TranslatePress - Personal (1.3.8+).

Best regards,

@razvan-translatepress
Copy link

Hi again,

Just wanted to notify you that we released the update containing the previously mentioned fix.

Thank you again for reporting this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3rd party compatibility Issues related to 3rd party compatibility like theme, plugin or hosting
Projects
None yet
Development

No branches or pull requests

3 participants