WordPress - Transform query parameter URL to clean permalink structure

I’m building a WordPress plugin and need assistance with URL rewriting. I have a business directory where users can click on business names to view detailed information. Instead of using WordPress post types, I created a custom database table to store the business data.

Currently, my listing page generates links like this:

<a href="http://example.com/directory/profile/?business=<?php echo $row->business_title; ?>">View Details</a>

// $row->business_title comes from my custom table

This creates URLs that look messy:

http://example.com/directory/profile/?business=ABC%20Marketing%20Solutions

On the profile page, I use $_GET['business'] to fetch the business name and query my database for the corresponding details.

However, I want to create clean, SEO-friendly URLs instead:

http://example.com/directory/profile/abc-marketing-solutions

I still need to extract the business name from this clean URL format and use it to retrieve data from my database. What’s the best approach to implement this URL transformation in WordPress while maintaining the functionality?

totally agree! using wp_rewrite is def the way to go. just add_rewrite_rule to set up your pretty permalinks, and remember to flush those permalinks or it’ll cause chaos down the line. good luck!

Had this same issue building a restaurant directory plugin last year. Here’s what worked for me - do these steps in order or you’ll hate yourself later. First, create a custom endpoint with add_rewrite_endpoint() when your plugin loads. Use sanitize_title() for clean URLs - it’s way better than trying to manually replace special characters. For the data retrieval, hook into template_redirect and check if your endpoint’s active with get_query_var(). Big gotcha: business names aren’t always unique, so append an ID or build a proper slug system. Don’t forget 404 handling for missing businesses or you’ll get broken links everywhere. The WordPress Codex has some great examples for custom rewrite rules that’ll save you hours of debugging.

Been there with URL rewriting headaches. The manual WordPress rewrite approach works, but it gets messy fast with custom database queries and complex business logic.

I had a similar project - a property listing system that needed clean URLs. Instead of wrestling with WordPress rewrite rules and custom endpoints, I built an automation that handles everything.

Someone clicks a business link? The automation catches the messy URL, transforms it to clean format, and updates your database queries in the background. No manual sanitize_title() or permalink conflicts to worry about.

Best part: webhooks automatically generate clean URLs when new businesses hit your database. It handles edge cases too - duplicate business names get unique slugs automatically.

This separates URL logic from WordPress core functions. You avoid the common pitfalls with rewrite rule priorities and caching issues that kill custom plugins.

Check out how to build this automation flow: https://latenode.com

quick tip - handle url encoding properly when converting slugs back to business names. i’ve seen special characters break database lookups even when sanitize_title works fine.

You can achieve clean URLs with custom rewrite rules in your plugin’s activation hook. Utilize add_rewrite_rule() to set up a rule that matches your desired URL format, allowing you to extract the business name as a query variable. Don’t forget to register this variable with the query_vars filter. In your profile template, instead of using $_GET[‘business’], retrieve it with get_query_var(). When generating links, convert spaces to hyphens, and reverse this when fetching from your database. It’s crucial to flush the rewrite rules during activation to avoid permalink conflicts, ensuring your rule’s priority is appropriate to work seamlessly with WordPress’s existing routes.