Skip to content

Instantly share code, notes, and snippets.

@petenelson
Created February 6, 2013 19:14
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save petenelson/4724984 to your computer and use it in GitHub Desktop.
Save petenelson/4724984 to your computer and use it in GitHub Desktop.
WordPress: Create a REST API endpoint
<?php
/*
* Sample code for using WordPress as a REST API endpoint (vs AJAX Admin)
* Author: Pete Nelson @GunGeekATX
*
* 1) Create a page called API in WordPres
* 2) Create a file called page-api.php in your theme directory
* 3) Add code as-needed
*
*/
// See what API call we want to do
// example: http://localhost:/wp-mysite/api/?action=list-all-pages
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
$output = null;
$sample_api = new My_Sample_API();
switch ($action) {
case 'list-all-pages':
$output = $sample_api->list_all_pages();
break;
default:
$output = array('error' => 'invalid action');
break;
}
if ($output) {
// callback support for JSONP
if (isset($_REQUEST["callback"])) {
header("Content-Type: application/javascript");
echo $_REQUEST['callback'] . '(' . json_encode($output) . ')';
}
else {
header("Content-Type: application/json");
echo json_encode($output);
}
}
die();
class My_Sample_API {
function list_all_pages() {
// last three options are from
// http://www.billerickson.net/code/improve-performance-of-wp_query/
$query = new WP_Query(
array(
'post_type' => 'page',
'no_found_rows' => true, // counts posts, remove if pagination required
'update_post_term_cache' => false, // grabs terms, remove if terms required (category, tag...)
'update_post_meta_cache' => false, // grabs post meta, remove if post meta required
)
);
$output = array();
// output just a small subset of the page information
while ($query->have_posts()) {
$p= $query->next_post();
// we'll return just a subest of
$output[] = array(
'id' => $p->ID,
'title' => $p->post_title,
'post_date_gmt' => $p->post_date_gmt,
'permalink' => get_permalink( $p->ID ),
);
}
return $output;
}
}
@damusix
Copy link

damusix commented Oct 10, 2013

How can I make it so that I do page-api.php from a plugin instead of the template?

@sweetnight
Copy link

Curious about that too. I guess you must use some conditional tags or make some shortcode tags inside the page.

@meglio
Copy link

meglio commented Aug 7, 2015

Alternatively, try API Endpoints plugin that lets you add new api to WordPress without PHP coding.

@tommytx
Copy link

tommytx commented Dec 28, 2015

i recently had an autocomplete input box fail and we were using ajax admin as the api endpoint.. when it failed we changed to api end point as shown below..
add_filter( 'idx_plus_search_widget_js', 'idx_plus_autocomplete_use_wp_ajax_endpoint' );
/**

  • Use WP AJAX endpoint you are having a plugin conflict when directly accessing this file for search results
  • @param array $settings JS localization array
  • @return array Array, modified with new url parameter
    */
    function idx_plus_autocomplete_use_wp_ajax_endpoint( $settings ) {
    $settings[ 'url' ] = admin_url( 'admin-ajax.php' );
    return $settings;
    }

after that it all worked well.. now with the arrival of wp 4.4 and we know they added the restful api.. I am wondering if that could have screwed up the api endpoint as the same problem has returned.. it does not offer any choices.. just says not found...when trying to enter text.
Do you have any tidbits of info that might be helpful...
as you can see here http://mlslistingstampa.com/listings/2109-w-hills-ave-unit-c-tampa-fl/ the second autocomplete box on the right gives a not found.. the top one works but its totally different.. this failed when wp 4.4 arrived.. and has been proven..
if you want to see what a working non 4.4 site looks like here it is..http://foreclosure-city.info
I am desperate and would pay for help on this one..

@tommytx
Copy link

tommytx commented Dec 28, 2015

Is it possible they changed the admin-ajax.php which stopped the autocomplete..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment