Dynamic Endpoints

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/?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x