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

canonical-presenter hook removes final slash in URL if URL contains a period #21371

Closed
2 of 11 tasks
MatthewSbar opened this issue May 7, 2024 · 3 comments
Closed
2 of 11 tasks

Comments

@MatthewSbar
Copy link

MatthewSbar commented May 7, 2024

  • I've read and understood the contribution guidelines.
  • I've searched for any related issues and avoided creating a duplicate issue.

Please give us a description of what happened

I have pages where I need to override the canonical - if I have a URL without any periods whatsoever, everything works fine, but if my URL includes a period, the final slash in the URL gets removed for some reason

To Reproduce

Step-by-step reproduction instructions

Add a filter like so (assuming the site is https://www.yubico.com)

function yubicool_wwyk_canonical($canonical) {
    $canonical = home_url('/works-with-yubikey/catalog/relate.it/');
    echo $canonical; // This prints https://www.yubico.com/works-with-yubikey/catalog/relate.it/
    return $canonical; // The canonical is set to https://www.yubico.com/works-with-yubikey/catalog/relate.it <- No trailing slash
}
add_filter('wpseo_canonical', 'yubicool_wwyk_canonical');
  1. Check the canonical URL of the page

Expected results

  1. The echoed URL is equivalent to the canonical URL

Actual results

  1. The canonical URL is missing a trailing slash

Screenshots, screen recording, code snippet

If possible, please provide a screenshot, a screen recording or a code snippet which demonstrates the bug.

Technical info

  • If relevant, which editor is affected (or editors):
  • Block Editor
  • Gutenberg Editor
  • Elementor Editor
  • Classic Editor
  • Other:
  • Which browser is affected (or browsers):
  • Chrome
  • Firefox
  • Safari
  • Other:

Used versions

  • Device you are using: (docker) ubuntu:focal
  • Operating system: ubuntu:focal
  • PHP version: 7.4
  • WordPress version: 7.4
  • WordPress Theme: 5.6.3
  • Yoast SEO version: 22.6
  • Relevant plugins in case of a bug:
@josevarghese
Copy link
Contributor

Hi @MatthewSbar

Thanks for using the Yoast SEO plugin and also for creating the issue. I tried to reproduce the issue by creating a URL with a period within the slug. Unfortunately, I noticed that WordPress is changing the period to - hypen automatically. So, is there any way to replicate this, as WordPress seems to be not allowing the period in the slug by default?

We look forward to hearing from you.

@MatthewSbar
Copy link
Author

MatthewSbar commented Jun 17, 2024

I suppose I should clarify that these aren't "normal" WordPress pages, I'm using PHP with a rewrite rule to load a page template, then using a bunch of PHP to overwrite Yoast's metadata with my own. It's a convoluted setup that's required for my use-case of having a series of pages populated by an API living within WordPress. I think the amount of code I'd have to share to have you reproduce is a bit excessive and certainly I'm the only person in the world who will have this issue, so, you can probably close it.

I worked around it by just asking the API provider to not-use URLs with periods in them which thankfully worked.

If you really want to reproduce, you can do something like this in a plugin (some of this might need tweaking since I renamed/removed as much as possible, your mileage may vary)

add_action('init', 'my_rewrite_rule');
function my_rewrite_rule() {
    add_rewrite_rule('^catalog\\/[\w.-]', 'index.php?my_param=1', 'top');
}

add_action('query_vars', 'template_set_query_var');
function template_set_query_var($vars) {
    array_push($vars, 'my_param');
    return $vars;
}

add_filter('template_include', 'include_template');
function include_template($template) {
    if (get_query_var('my_param')) {
        $my_template = get_template_directory() . '/my-template.php';
        if (file_exists($my_template)) {
            $template = $my_template;
        }
    }
    return $template;
}

function get_url_key() {
    $url_key = trim($_SERVER['REQUEST_URI'], '/');
    $url_key = explode('/', $url_key);
    $url_key = end($url_key);
    return $url_key;
}

function fix_canonical($canonical) {
    if (get_query_var('my_param')) {
        $canonical = home_url('/catalog/' . get_url_key() . '/');
    }
    return $canonical;
}
add_filter('wpseo_canonical', 'fix_canonical');

You'll also need a template file in your theme folder like so

<!DOCTYPE html>

<head>
    <?php wp_head(); ?>
</head>

<body>
    hello world
</body>

<?php wp_footer(); ?>

Then don't forget to flush rewrite rules. :)

If you jump through all these hoops you should be able to visit /catalog/whatever-you-want/ and it'll load your template. Your canonical will be correct. If you visit /catalog/whatever-you-wa.nt/ the canonical will be missing a trailing slash

@josevarghese
Copy link
Contributor

Hi @MatthewSbar

Thanks for replying and letting us know you found a fix for the issue. Since it won't occur with the normal WordPress setup, I'm closing this issue for now as we work on the high-priority issues first.

If more people report this in the future, we will reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants