Skip to main content
Added context to the issue
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175

The Situation

I'm working with an external API that gives me an XML feed of items and categories. The API works with category IDs and returns items in that category. What I'm trying to do is the following:

  • Make an endpoint products which shows all the available categories: domain.com/products/
  • Make a dynamic endpoint that passes the category ID via query string ?custom_category=2 and rewrites it into a pretty permalink like domain.com/products/categoryname/ - I don't know the category names so I need to ask the API the category name by the given ID.
  • Finally, in a similar fashion as above I need to take a given item ID via query string item_id=17 and rewrite it into a pretty permalink of which I'm not picky: domain.com/products/productname/ or domain.com/products/categoryname/productname/ - I don't know the item names so I need to ask the API the item name by the given ID.

I have to use IDs in links because the API is old and doesn't understand slugs ( so I can't pass my API a slug and expect results I need to pass an ID of some kind ) but I was hoping to grab the ID from the query string, hit the API to get the name, make it URL friendly and put it into the URL.


The Situation

I'm working with an external API that gives me an XML feed of items and categories. The API works with category IDs and returns items in that category. What I'm trying to do is the following:

  • Make an endpoint products which shows all the available categories: domain.com/products/
  • Make a dynamic endpoint that passes the category ID via query string ?custom_category=2 and rewrites it into a pretty permalink like domain.com/products/categoryname/ - I don't know the category names so I need to ask the API the category name by the given ID.
  • Finally, in a similar fashion as above I need to take a given item ID via query string item_id=17 and rewrite it into a pretty permalink of which I'm not picky: domain.com/products/productname/ or domain.com/products/categoryname/productname/ - I don't know the item names so I need to ask the API the item name by the given ID.

I have to use IDs in links because the API is old and doesn't understand slugs ( so I can't pass my API a slug and expect results I need to pass an ID of some kind ) but I was hoping to grab the ID from the query string, hit the API to get the name, make it URL friendly and put it into the URL.

Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175

Enpoint Not Found in Rewrite Tag Permalink

I'm trying to make an XML feed show nice and pretty permalinks rather than showing query strings. The problem is I can't get my /products/ endpoint to show up when trying to process my custom rewrite template tag. Here's what I have:

/** Register Query Vars **/
function theme_custom_query_vars( $vars ){
    $vars[] = 'custom_category';
    $vars[] = 'products';
    return $vars;
}
add_filter( 'query_vars', 'theme_custom_query_vars' );

/** Register Endpoint **/
function theme_register_endpoints() {
    add_rewrite_rule( '^products/?', 'index.php?products=products', 'top' );
    add_rewrite_rule( '^products/([^/])/?', 'index.php?products=products&custom_category=$matches[2]', 'top' );
    
    add_rewrite_tag( '%custom_category%', '([0-9])' );
}
add_action( 'init', 'theme_register_endpoints' );

That's fine - my products endpoint exists and I can redirect it to a specific template. Now the issue is that while wp_query knows that custom_category exists and has a value assigned to it, the passed $permalink only shows my base URL ( http://domain.com/ ) so I cannot replace the template tag since it doesn't exist in my $permalink variable.

/** Process our Rewrite Tag **/
function theme_rewrite_tags_filter( $permalink ) {
    global $wp_query;
    printf( '<pre>%s</pre>', print_r( $wp_query, 1 ) );
    die( $permalink );

    if( false !== strpos( $permalink, '%custom_category%' ) ) {
        die( 'made it!' );
    }
 
    return $permalink;
}
add_filter( 'page_link', 'theme_rewrite_tags_filter' );

I gotta be missing something in regards to template tags or maybe I have a misunderstanding of how endpoints are structured but the $permalink just isn't showing the products endpoint or the %custom_category% template tag: