Skip to content

Instantly share code, notes, and snippets.

@nickfox
Created July 17, 2014 01:07
Show Gist options
  • Select an option

  • Save nickfox/bccdabbb25a0cc052c2d to your computer and use it in GitHub Desktop.

Select an option

Save nickfox/bccdabbb25a0cc052c2d to your computer and use it in GitHub Desktop.

Revisions

  1. nickfox created this gist Jul 17, 2014.
    53 changes: 53 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <?php
    defined('ABSPATH') or exit;
    /*
    Plugin Name: Gps Tracker Endpoint
    Plugin URI: https://www.websmithing.com/gps-tracker
    Description: Create an endpoint for Gps Tracker.
    Version: 1.0.0
    Author: Nick Fox
    Author URI: https://www.websmithing.com/hire-me
    License: GPL2
    */

    function makeplugins_endpoints_add_endpoint() {
    // register a "gpstracker" endpoint to be applied to posts and pages
    add_rewrite_endpoint( 'gpstracker', EP_ALL );
    }
    add_action( 'init', 'makeplugins_endpoints_add_endpoint' );

    function makeplugins_endpoints_template_redirect() {
    global $wp_query;

    // if this is not a request for gpstracker or it's not a singular object then bail
    if ( ! isset( $wp_query->query_vars['gpstracker'] ) || ! is_singular() )
    return;

    // output some json (normally you might include a template file here)
    makeplugins_endpoints_do_gpstracker();
    exit;
    }
    add_action( 'template_redirect', 'makeplugins_endpoints_template_redirect' );

    function makeplugins_endpoints_do_gpstracker() {
    //header( 'Content-Type: application/json' );

    //$post = get_queried_object();
    //echo gpstracker_encode( $post );

    echo '137';
    }

    function makeplugins_endpoints_activate() {
    // ensure our endpoint is added before flushing rewrite rules
    makeplugins_endpoints_add_endpoint();
    // flush rewrite rules - only do this on activation as anything more frequent is bad!
    flush_rewrite_rules();
    }
    register_activation_hook( __FILE__, 'makeplugins_endpoints_activate' );

    function makeplugins_endpoints_deactivate() {
    // flush rules on deactivate as well so they're not left hanging around uselessly
    flush_rewrite_rules();
    }
    register_deactivation_hook( __FILE__, 'makeplugins_endpoints_deactivate' );