Search code examples
wordpresspluginsurl-rewritingpermalinks

Wordpress add rewrite rule doesn't work for me


I'm developing a plugin that creates 2 pages: one listing page and one details page.

I don't use custom types. I have created a table into the database where I save the company details and also a slug for the details page.

It's working properly but I have a problem with the URLs. They are not pretty.

On the listing page I use this code to create the link:

    <a href="https://www.website.com/company/details/?companyname=<?php echo $value->company_slug; ?>"><?php echo stripslashes($value->company_name); ?></a>

The generated link looks like this:

    https://www.website.com/company/details/?companyname=new-company-name

I use the query because I need it on the details page, where I use this code:

    $company_slug = $_GET['companyname'];
    $company_details = $wpdb->get_row("SELECT * FROM $table_company WHERE company_slug = '$company_slug'");

This is how I retrieve the company details from sql and it also works just fine.

I have created manually in Wordpress the details page.

The problem is that I want the details URL to look pretty, like this:

    https://www.website.com/company/details/new-company-name/

Generating it like this it's easy but when I click, I get a 404, since the query from URL is missing.

I was thinking it's easy to create directly the pretty URL and on the details page to parse the URL and get the company slug. It didn't work. I get a 404 maybe because the page doesn't physically exist.

So, I've done some research about URL rewrite and I have found some examples but none worked.

I have found tried this code also:

    add_filter('query_vars', function($vars) {
        $vars[] = "companyname";
        return $vars;
    });

    function custom_rewrite_rule() {
        add_rewrite_rule('^companyname/?([^/]*)/?','company/details/?companyname=$matches[1]','top');
    }
    add_action('init', 'custom_rewrite_rule', 10, 0);

I've read that I shouldn't use matches if I use a custom URL instead of index.php, so I have also tried without matches:

    add_rewrite_rule('^companyname/?([^/]*)/?','company/details/?companyname=$1','top');

No results. If course, after every change I have saved again the permalinks.

This should be an easy task but somehow it doesn't work.

    https://developer.wordpress.org/reference/functions/add_rewrite_rule/

Does anyone of you know how can I make this work?

Thank you.

Regards, AG


Solution

  • I am assuming that the details page you created has the slug company/details. Here's what you need to do to make it work-

    1. Add the custom rewrite rules in functions.php file:

    function theme_custom_rewrites() {
        add_rewrite_tag("%companyname%", "([a-z0-9\-_]+)");
      add_rewrite_rule('^company/details/([a-z0-9\-_]+)/?$', 'index.php?pagename=company/details&companyname=$matches[1]', 'top');
    }
    
    add_action('init', 'theme_custom_rewrites'); 
    

    It registers a new rewrite tag/query var named companyname to be used later and registers a custom rewrite rule for the specific URL structure you want (/company/details/company_name).

    2. Get the company name on the template file and use it: After you have added the above code and saved the permalinks, you can get the companyname just by using the get_query_var() function.

    $companyname = get_query_var( 'companyname' );

    Hope it helps. Thanks.