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?
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 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 ).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With