WordPress.org

Ready to get started?Download WordPress

Forums

Custom Plugin Permalink (4 posts)

  1. jeremib
    Member
    Posted 1 year ago #

    So here's what I'm trying to do. I'm building a plugin for a business directory. Each business is assigned to a category. There are two views, listing and detail. To give the user as much flexibility as possible, I have created a shortcode where they can pass in the category and that will display the listview. This works great. They can create a page, called "Fishing" with a permalink of "/fly-fishing" and the content is

    Here's some title information about our fishing.  Thanks yadda yadda.
    [business-directory category=fishing]

    Notice that the category and the permalink are different. Again, to show the flexibility I'm trying to achieve.

    In my listings, I would like a permalink to each business that uses the permalink of the category page that they select. So one might be /fly-fishing/abc-fishing. When that permalink is visited, I need my plugin to know that the category is actually fishing and the business is abc-fishing.

    I'm having trouble with creating the add_rewrite_rule such that it knows what page to pass back to. My thought was to query each page, look for my shortcode, and then add the rewrite_rules accordingly. This means I will have to do this on every page creating/update.

    Is there a better way?

  2. bcworkz
    Member
    Posted 1 year ago #

    I think I understand your concept, except for how the permalink values are equated to a category. Who assigns this and how and where is this relationship maintained?

  3. jeremib
    Member
    Posted 1 year ago #

    My plugin calls a web service and passes the category to get the business listings.

    The permalink values can, but don't have to be, related to the category. That's what I'm trying to achieve. I can get a list of a the categories from the web service, and build my permalink structures when the plugin is installed, but that limits the permalinks to have to include the categories.

    What I'm trying to achieve is something where someone could create a page, with a permalink of /shopping, then have

    <h1>Fishing</h1>
    [business-directory category=fishing]

    The URLS generated from my plugin would know that even though the category is fishing, the "parent page" is /shopping, so my permalinks would be /shopping/abc-shop and /shopping/baitshop instead of /fishing/abc-shop

  4. bcworkz
    Member
    Posted 1 year ago #

    OK, I'm fairly sure I have a good enough picture now. If the permalink structure is such that the category is a particular level and the business is another level, you might accomplish your goals with one rewrite rule. Let's say your permalink structure is domain.com/directory/category/business/ . Add this rule:
    add_rewrite_rule('^directory/([^/]*)/([^/]*)/?','directory.php?cat=$matches[1]&biz=$matches[2]','top');
    Your directory.php page will first require_once wp-load.php. It would then build the proper query to your web service to find an existing category match to the "cat" parameter value. If that's not possible, it could search a custom local table containing such information.

    Depending on if there's a match or not, it combines these results with a similar process with the "biz" parameter value and does whatever needs to happen to build the "real" WP query that will return the results that you desire.

    Regardless if I properly interpreted you needs, you can see this directory.php page can manage all manner of permalink possibilities, regardless of the logic needed, as long as it can be coded, it can transform the request into a meaningful query. So instead of trying to develop rewrites to handle every possibility, just pass the values to a proper page where code can process the values with much more complexity than what could ever be possible with rewrite rules alone.

Topic Closed

This topic has been closed to new replies.

About this Topic