0

I wrote a plugin that can fetch data from an external JSON source and displays a very few details in a list. Each item is clickable to be displayed with more details on a specific template. To do this, each item has its own URL like this : http://localhost/details/?id=125

The get parameter is meant to retrieve more data from the JSON source thanks to query_vars. It works without a hitch but I would like URLs to be rewrote this way : http://localhost/details/name-of-the-item/id

Is that possible ? How can I do this ? (I can pass as many get parameters as possible from the list page if needed)

1 Answer 1

1

You can try URL rewriting like that

add_action("wp_loaded", function () {


    add_rewrite_tag("%id%", "([^&]+)");

    add_rewrite_rule(
        '^details/[^&]+/([0-9]+)/?',
        'index.php?id=$matches[1]&pagename=details',
        'top'
    );


});

And then flush rewrite rules cache one time in Settings -> Permalinks.

With this rewriting, the identifier is no more in $_GET but you can get it with https://codex.wordpress.org/Function_Reference/get_query_var.

2
  • 2
    It would be better to use get_query_var(), rather than accessing the global directory. Aug 1, 2018 at 11:25
  • 1
    thank you @JacobPeattie I was searching for the function "get_query_vars" and because I don't found it, I though there is no other solution than global var.
    – Kaperto
    Aug 1, 2018 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.