7

I have a database table outside WordPress that I need to make endpoints for. I've created a page /cars/ where I plan on using a page template to generate links. I would like for the url to look like /cars/camaro/ ( /cars/%model%/ ). Originally I thought I could do it with endpoints but wasn't sure how to make them dynamic based off of the slug pulled from the Model Table. I also wasn't sure if it would be easier using Permalink Structure tags or which was better.

I'm not even entirely sure where to start, I've created "static" endpoints before so I had a base to jump off of but when I get to the request filter I'm not sure what to do.

/**
 * Add endpoint to handle models
 */
function theme_endpoints() {
    add_rewrite_endpoint( 'cars', 'EP_PAGES' );     // Since cars is our base
}
add_action( 'init', 'theme_endpoints' );

/**
 * Filter request variables
 * @param array $vars
 * @return array $vars
 */
function theme_perma_requests( $vars ) {

    if( isset( $vars['model'] ) ) {
        $vars['model_template'] = true;
    }

    return $vars;
}
add_filter( 'request', 'theme_perma_requests' );

I mean, this allows me to my template_include ( which is why I added the model_template variable ) but it doesn't actually rewrite the URL. So then I started playing with add_rewrite_rule()

/**
 * Theme rewrite rules
 */
function theme_rewrite_rules() {
    add_rewrite_rule( '^cars/([^/]*)/?$', 'index.php?page_id=8&model=$matches[0]', 'top' );
}
add_action( 'init', 'theme_rewrite_rules' );

I've flushed my permalinks but the rewrites don't seem to take effect, I can only assume something's wrong with how the rule is written.


Do I need both the endpoint and the rewrite rule or just one of the two? Am I going about it all wrong? How can I achieve the permalink structure of /cars/camaro/?

  • I do not have much experience with this but I think add_rewrite_tag() is a good starting point. – Sisir Jul 8 '15 at 18:25
5

10up engineering best practices as a great example on how to achieve that. You basically add a new rewrite tag and a new rewrite rule and then using the template_redirect action you return your custom action.

Here is the code:

add_action( 'init', function() {
  add_rewrite_tag( '%model%', '([^/]+)' );
  add_rewrite_rule( 'cars/([^/]+)/?', 'index.php?model=$matches[1]', 'top' );
} );

add_action( 'template_redirect', function() {
  global $wp_query;

  $model = $wp_query->get( 'model' );

  if ( ! empty( $model ) ) {
    // Return stuff here
  }
} );

Don't forget to flush permalinks before trying your new endpoint! Using wp-cli:

wp rewrite flush

Or using the admin UI: http://example.local/wp-admin/options-permalink.php and click save.

  • I forgot that this question existed and ran across it again looking for a solution. Seems like this totally works - clean, simple, and straightforward surprisingly. Does 10up have a tutorial on this you can link to? Thanks a ton! – Howdy_McGee Jan 25 '16 at 21:34
  • 1
  • Thank you for thi solution. It absolutley what I was looking for. But I need just a bit more help with my theme. Right now this rule sending the user to my index.php, while I want the user to land on specific page , and just use the querystring param. For example: mywebsite.com/parentpage/childpage/somevalue/ to actually go to : mywebsite.com/parentpage/childpage/ and "somevalue" will be used as a variable inside this page. Can this be achieved using this method? Thanks! – Shir Gans Mar 22 '16 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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