Skip to content

Instantly share code, notes, and snippets.

@petenelson
Created February 6, 2013 19:14

Revisions

  1. petenelson revised this gist Feb 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion page-api.php
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@
    // callback support for JSONP
    if (isset($_REQUEST["callback"])) {
    header("Content-Type: application/javascript");
    echo $_GET['callback'] . '(' . json_encode($output) . ')';
    echo $_REQUEST['callback'] . '(' . json_encode($output) . ')';
    }
    else {
    header("Content-Type: application/json");
  2. petenelson revised this gist Feb 6, 2013. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions page-api.php
    Original file line number Diff line number Diff line change
    @@ -28,8 +28,17 @@
    break;
    }

    if ($output)
    echo json_encode($output);
    if ($output) {
    // callback support for JSONP
    if (isset($_REQUEST["callback"])) {
    header("Content-Type: application/javascript");
    echo $_GET['callback'] . '(' . json_encode($output) . ')';
    }
    else {
    header("Content-Type: application/json");
    echo json_encode($output);
    }
    }

    die();

  3. petenelson revised this gist Feb 6, 2013. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions page-api.php
    Original file line number Diff line number Diff line change
    @@ -53,14 +53,17 @@ function list_all_pages() {

    $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,
    'permalink' => get_permalink( $p->ID )
    );
    'post_date_gmt' => $p->post_date_gmt,
    'permalink' => get_permalink( $p->ID ),
    );

    }

  4. petenelson created this gist Feb 6, 2013.
    72 changes: 72 additions & 0 deletions page-api.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    <?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)
    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();

    while ($query->have_posts()) {
    $p= $query->next_post();

    $output[] = array(
    'id' => $p->ID,
    'title' => $p->post_title,
    'permalink' => get_permalink( $p->ID )
    );

    }

    return $output;

    }

    }