我的Wordpress网站使用自定义模板页面,如下所示:
<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");
...
我的自定义页面project_costs.php
执行以下步骤:
- 从页面表单(POST/GET)接收和设置用户输入的变量。
- 从数据库拉取数据。
- 做一些计算和更改。
- 为用户创建页面。
我想将 angular.js 与 WP-API 插件集成。该插件只是从数据库中提取原始数据(步骤 2)并将其发送到前端(步骤 4)。因此,未用作页面的页面和模板不会重新加载。
我想先将数据传递给我的php类(步骤3),然后将更改的数据传递给WP-API。
WP-API 中是否有任何函数可以调用我的 PHP 文件或函数?
任何建议,样品或链接将不胜感激。
谢谢。
所以我
正在做一个庞大的项目,其中包括几个用于 #WordPress 的API/Angular替换部件。一个文件是自定义终结点样板.php。到目前为止,它就像一个魅力,但任何输入将不胜感激。
只需遵循结构并使用my_awesome_function
返回做任何您想做的事情。然后,来自挂钩的命名空间和路由将使用来自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;
}
因此,您的电话将成为get
http://yourproject.com/wp-json/custom-endpoint/v2/author/1