• Resolved Priyanka

    (@dev_205)


    Hi there,

    I’m facing the issue with the custom link which I have generated using “add_rewrite_tag” and “add_rewrite_rule” WP functions.

    The custom link automatically stopped working after updating the WP version to 5.5 and it was working perfectly in the past version of 5.4.2 and 5.4.1 so, I think the latest version(5.5) is having a bug related to rewrite rules.

    Kindly suggest.

    Thanks,
    Priyanka

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Please share the code you are using to add rewrite rules. I’ve not had any trouble with my rewrite rules.

    Thread Starter Priyanka

    (@dev_205)

    Hi,

    Please find the code below:

    
    function custom_rewrite_tag()
    {
    	add_rewrite_tag('%type%', '([^&]+)');
    }
    add_action('init', 'custom_rewrite_tag', 10, 0);
    
    function custom_rewrite_basic()
    {
    	$detail_pageid = 2;	
    	add_rewrite_rule('^actions/requests/([a-zA-Z0-9\-]+)/?', 'index.php?page_id='.$detail_pageid.'&page=requests&type=$matches[1]', 'top');
    }
    add_action('init', 'custom_rewrite_basic', 10, 0);

    Looking forward to your reply.

    Thanks!

    Moderator bcworkz

    (@bcworkz)

    I tried your rewrite rule code on my site and it works as expected. All I changed was the page ID value to match one of my pages.

    Try visiting the permalinks settings screen, which forces WP to regenerate all of its rules. Maybe doing so will cause WP to recognize your rules again.

    If that fails, try switching to twentytwenty theme and deactivating all plugins. Temporarily place your rewrite tag and rule code in twentytwenty’s functions.php. Visit the permalinks settings screen again. Afterwards I’d expect your rule to be recognized. What happens on the page may fail due to missing resources, but you should at least get to the page. If this page is based on a custom template selected in page attributes, you may need to re-select it again once the proper template is again available.

    Then restore your theme and plugins, one at a time, revisiting permalinks settings and testing after each. When the rewrite fails again, the last activated module is the cause.

    Thread Starter Priyanka

    (@dev_205)

    Hi,

    Thanks for your reply! I will try the way you have suggested. But as far as I know, I have updated all the plugins so there’s might be less chances of conflict with any plugin still will check and let you know.

    Thanks!

    Thread Starter Priyanka

    (@dev_205)

    Hi @bcworkz,

    As you have suggested, I have switched the theme to twentytwenty and deactivated all plugins. I have added the following code in the twentytwenty theme’s function file.

    add_action('init', 'custom_rewrite_tag', 10, 0);
    function custom_rewrite_tag() {
    add_rewrite_tag('%catslug%', '([^&]+)');
    }
    
    add_action('init', 'custom_rewrite_basic', 10, 0);
    function custom_rewrite_basic() {
    	$prolist_pageid = get_id_by_slug('propositions');	
    
    	add_rewrite_rule('^propositions/([a-zA-Z0-9\-]+)/?', 'index.php?page_id='.$prolist_pageid.'&page=propositions&catslug=$matches[1]', 'top');
    
    }

    What exactly I want to need and how I’m doing it for that is:

    I am already having a page named “propositions” and it has the same slug (propositions). Now, by using a rewrite function, I want to add the extra slug just after the “propositions” so that I have taken the “catslug” parameter as you can see in the above code. After adding the code, I’m updating the permalink by hitting the save button only.

    I want this URL to be worked: http://sitename.com/propositions/test-proposal
    where the “test-proposal” is not an actual page but I’m passing it in the URL as a “catslug” parameter.

    The above URL should work and it was worked in the past version than WP 5.5 but now it’s not working and when I run that, it’s automatically redirected to http://sitename.com/propositions/ page.

    Hope you understand what I explained and help with that.

    Thanks!
    Priyanka.

    Moderator bcworkz

    (@bcworkz)

    The page name parameter should be pagename=propositions. You do not need both page_id and pagename parameters, I advise removing one or the other as redundant parameters can possibly confuse WP.

    After making changes, don’t forget to visit the permalinks settings screen to cause the rewrite rules to be regenerated.

    WP redirects requests to the resource’s canonical URL. You can prevent that from happening with the “canonical_url” filter. The original and canonical URLs are passed to your callback. Return the original to prevent the redirect.

    Thread Starter Priyanka

    (@dev_205)

    Hi @bcworkz,

    Thanks for your help!

    It works now with “pagename”, although it’s working without “canonical_url” filter.

    Anyway thanks again.

    Regards,
    Priyanka

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom rewrite rules stopped working after updating WP 5.5’ is closed to new replies.