• Hi all,

    I am developing a plugin, that has a list shortcode that also has an option to filter list items within a category. I want to make a clean SEO URL no mater where the shortcode is put by the user. e.g. example.com/{$page-title}/{$items-category} instead of example.com/{$page-title}/?category_id=15, how can I achieve this without creating custom post types? Or perhaps someone can advise a better solution to do this?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Here are all the rewrite functions: https://developer.www.ads-software.com/?s=rewrite
    You can use add_rewrite_rule to do what you are saying, but I don’t see how the URL affects the shortcode (or how your question sounds like you want the shortcode to affect the URL, which it can’t).

    But to have the shortcode work anywhere, it should not be tied to a rewrite rule. Page titles are in the rewrite rules, and posts have their own permalink structure which has post type and taxonomy variables that get parsed out. Anything you add should be unique from all the existing rewrite stuff, but if you have a shortcode, its parameters would tell it what to do, not the URL parameters. The shortcode might get its default values from the page it is on, but that is it.

    Thread Starter Marius

    (@mariusslo)

    Okay, so I think I may have provided too little details on this matter. Let me try again so the issue might be clearer.

    To answer your question how the shortcode should affect the url and vice versa (we are talking about the content that the shortcode prints out on the page not the shortcode itself). The shortcode prints out an entire list of items and a categories dropdown which on change should trigger a GET method on the url, in this case after selecting the option from the dropdown it sets $_GET['category_id'], which then affects the content of the shortcode list, listing only the items that are associated with the selected category id. Without any rewrite rules the url turns into example.com/{$page_title}/?category_id=15 which isn’t good for SEO. So i would like to have example.com/{$page-title}/{$items-category}

    An alternative way of doing this would be to create a custom post type, e.g. items, which would have different shortcodes with category_id parameter for each category, but that just doesn’t seem right to me at the moment. To much hassle for the user and seems unnecessary.

    Moderator bcworkz

    (@bcworkz)

    add_rewrite_rule() is what you want to have WP recognize such an URL, but the URL structure you propose is going to conflict with existing rewrite rules for hierarchical page permalinks. You’d need some static element in your URLs which the rewrite rule’s regexp can match against to differentiate from a hierarchical page permalink. For example: example.com/{$page-title}/item-cat/{$items-category}

    With such a structure, no one should try to create a child page whose slug is “item-cat”, which shouldn’t be much of a hindrance. Plus, your rewrite rule must be evaluated first or WP will try to find a non-existent child page instead of an item category. Do so by passing 'top' as the 3rd parameter of add_rewrite_rule().
    https://developer.www.ads-software.com/reference/functions/add_rewrite_rule/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Rewrite page url where a shortcode is used’ is closed to new replies.