questions

problem in passing variables in url with wordpress 5.5.3

Rate this post

I’ve a page named with “profile”, and want to pass some variables in URL. for example:

http://localhost/wordpress/profile/?page=settings&param=social

And I want to pass all this vars like this:

http://localhost/wordpress/profile/settings/social

Here is my code:

add_action('init', function () {
add_rewrite_rule(
    'profile/([^/]*)/?([^/]*)/?$',
    'index.php?pagename=profile&page=$matches[1]&param=$matches[2]',
    'top'
);
});
add_filter('query_vars', function ($vars) {
        $vars[] = "page";
        $vars[] = "param";
        return $vars;
});

this code works fine in wordpress v5.4.4, but it doesn’t work after update to v5.5+. Latest version I’ve checked was 5.5.3, but still couldn’t figure out where is the problem.

Would you help me to find out where is the problem?

 

✔️Solution:

OK, since 2 months ago that I’ve asked this question, I’ve finally found solution to fix this issue. In new version of wordpress, a changes made by developer and somehow “page” variable is reserved. so you can fix this issue by rename it to whatever you want. so my code changed to this:

add_action('init', function () {
add_rewrite_rule(
    'profile/([^/]*)/?([^/]*)/?$',
    'index.php?pagename=profile&my_page_var=$matches[1]&param=$matches[2]',
    'top'
);
});
add_filter('query_vars', function ($vars) {
        $vars[] = "my_page_var";
        $vars[] = "param";
        return $vars;
});

and don’t forget this: go to Permalink Settings and hit the save change button. if you asking why, cause it will rewrite permalink rules and you need it, trust me 😉

this simple solution works fine to me in wordpress v5.6. hope it will do the same job for you.

Leave a Reply

Your email address will not be published.

Back to top button