2

My Wordpress site uses custom template pages like this one:

<php
 /* 
  Template Name : project_costs
 /*

get_header ();

// set default vars or user received
require("my_forms.php");
// pull data from db and calculate
require("project_calculation. php");
// create page
require("content. php");
...

My custom page project_costs.php performing the steps :

  1. Receive and set user entered vars from page forms (POST/GET).
  2. Pull data from database.
  3. Do some calculations and changes.
  4. Creates page for user. 

I want to integrate angular.js with the WP-API plugin. The plugin just pulls raw data from database (step 2) and sends it to front end (step 4). So pages and templates not in use as page didn't reload.

I want to pass data to my php class first (step 3), then pass changed data to WP-API.

Is there any function in WP-API to call my PHP file or function? 

Any advice, samples or links would be highly appreciated.

Thanks.

davmac
  • 20,150
  • 1
  • 40
  • 68
Mindaugas
  • 23
  • 1
  • 3
  • I'm not sure what you want. You can create custom classes to add new functionality to the API – Skatox Jan 16 '16 at 14:42
  • Check [Hook AJAX in Wordpress](http://stackoverflow.com/a/19722427/1287812) – brasofilo Jan 17 '16 at 03:07
  • I install wp-api plugin. when I type in address bar wp-json/v2/posts/122 I got full post and all post detales in json format. But if I type custom post id wp-json/v2/posts/125 I got json array where all post detales but instead of data "template" = "my template name.php". – Mindaugas Jan 21 '16 at 06:09
  • So instead of data just template name sent to browser which is useless. I want to execute that file and send output to browser – Mindaugas Jan 21 '16 at 06:45

1 Answers1

4

So I'm working on a huge project which includes several API/Angular replacement parts for #WordPress. One file is a custom endpoint-boilerplate.php. It works like a charm so far but any input would be appreciated.

Just follow the structure and use the my_awesome_function to return do anything you'd like. Then the namespace and route from the hook will be available, using data from my_awesome_func.

<?php 
/* ------------------------------------------------------------------------ *  
    A great example of custom endpoints is the PHP in wp-api-menus plugin
* ------------------------------------------------------------------------ */

// hook into rest_api_init and register a new api route
add_action( 'rest_api_init', function () {

register_rest_route( 
    'custom-endpoint/v2',   // namespace
    '/author/(?P<id>\d+)',  // route
    array(                  // options
        'methods'  => 'GET',
        'callback' => 'my_awesome_func',
        // 'args'     => array(
        //     'context' => array(
        //     'default' => 'view',
        //     ),
        // ) 
    )
);

});


 function my_awesome_func( $data ) {
    $posts = get_posts( array(
        'author' => $data['id'],
    ) );

    if ( empty( $posts ) ) {
        return null;
    }

    return $posts[0]->post_title;
}

So your call will then be a get to http://yourproject.com/wp-json/custom-endpoint/v2/author/1

Ben Racicot
  • 5,332
  • 12
  • 66
  • 130