php - Wordpress custom page template and WP-API -


my wordpress site uses custom template pages one:

<php  /*    template name : project_costs  /*  get_header ();  // set default vars or user received require("my_forms.php"); // pull data db , calculate require("project_calculation. php"); // create page require("content. php"); ... 

my custom page project_costs.php performing steps :

  1. receive , set user entered vars page forms (post/get).
  2. pull data database.
  3. do calculations , changes.
  4. creates page user. 

i want integrate angular.js wp-api plugin. plugin pulls raw data database (step 2) , sends front end (step 4). so pages , templates not in use page didn't reload.

i want pass data php class first (step 3), pass changed data wp-api.

is there function in wp-api call php file or function? 

any advice, samples or links highly appreciated.

thanks.

so i'm working on huge project includes several api/angular replacement parts #wordpress. 1 file custom endpoint-boilerplate.php. works charm far input appreciated.

just follow structure , use my_awesome_function return you'd like. namespace , route hook available, using data my_awesome_func.

<?php  /* ------------------------------------------------------------------------ *       great example of custom endpoints php in wp-api-menus plugin * ------------------------------------------------------------------------ */  // hook rest_api_init , register 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 call get http://yourproject.com/wp-json/custom-endpoint/v2/author/1


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -