Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a 'route' in wordpress?

For my own sanity I'm trying to create a route for an ajax api that looks something like:

/api/<action>

I'd like wordpress to handle this route and delegate to the proper action with do_action. Does wordpress give me a hook to implement this? Where's a good spot?

like image 471
Dane O'Connor Avatar asked Aug 26 '12 19:08

Dane O'Connor


People also ask

What is the difference between a route and an endpoint?

Routes vs Endpoints Endpoints perform a specific function, taking some number of parameters and return data to the client. A route is the “name” you use to access endpoints, used in the URL. A route can have multiple endpoints associated with it, and which is used depends on the HTTP verb.

How do I find my WordPress endpoints?

How We Find the Data: Follow the Route to an Endpoint. Accessing all of your site data via the REST API is as simple as composing a URL. For any WordPress site running at least version 4.7, add the following string to the end of your site's url: /wp-json/wp/v2 (e.g., http://example.com/wp-json/wp/v2 ).


1 Answers

You have to use add_rewrite_rule

Something like:

add_action('init', 'theme_functionality_urls');

function theme_functionality_urls() {

  /* Order section by fb likes */
  add_rewrite_rule(
    '^tus-fotos/mas-votadas/page/(\d)?',
    'index.php?post_type=usercontent&orderby=fb_likes&paged=$matches[1]',
    'top'
  );
  add_rewrite_rule(
    '^tus-fotos/mas-votadas?',
    'index.php?post_type=usercontent&orderby=fb_likes',
    'top'
  );

}

This creates /tus-fotos/mas-votadas and /tus-fotos/mas-votadas/page/{number}, that changes the orderby query var for a custom one, which I handle in the pre_get_posts filter.

New variables can also be added using the query_vars filters and adding it to the rewrite rule.

add_filter('query_vars', 'custom_query_vars');
add_action('init', 'theme_functionality_urls');

function custom_query_vars($vars){
  $vars[] = 'api_action';
  return $vars;
}

function theme_functionality_urls() {

  add_rewrite_rule(
    '^api/(\w)?',
    'index.php?api_action=$matches[1]',
    'top'
  );

}

Then, handle the custom request:

add_action('parse_request', 'custom_requests');
function custom_requests ( $wp ) { 

  $valid_actions = array('action1', 'action2');

  if(
    !empty($wp->query_vars['api_action']) &&
    in_array($wp->query_vars['api_action'], $valid_actions) 
  ) {

    // do something here

  }

}

Just remember to flush the rewrite rules by visiting /wp-admin/options-permalink.php or calling flush_rewrite_rules only when needed, since it's not a trivial process.

like image 176
davidmh Avatar answered Oct 02 '22 17:10

davidmh