Php Wordpress自定义页面模板和WP-API
我的Wordpress网站使用如下自定义模板页面:Php Wordpress自定义页面模板和WP-API,php,wordpress,wp-api,Php,Wordpress,Wp Api,我的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("
<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执行以下步骤:
从页面表单POST/GET接收并设置用户输入的VAR。
从数据库中提取数据。
做一些计算和改变。
为用户创建页面
我想将angular.js与WP-API插件集成。该插件只是从数据库第2步提取原始数据并将其发送到前端第4步。因此,未使用的页面和模板不会重新加载
我想在第一步3将数据传递给我的php类,然后将更改后的数据传递给WP-API
WP-API中有任何函数可以调用我的PHP文件或函数吗
任何建议,样品或链接将不胜感激
谢谢。所以我正在做一个庞大的项目,其中包括WordPress的几个API/Angle替换部件。其中一个文件是自定义端点-boilerplate.php。到目前为止,它就像一个符咒,但任何输入都将不胜感激 只需遵循结构并使用my_awesome_函数返回您想要的任何操作。然后,将使用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;
}
那么你的电话就成了一个http://yourproject.com/wp-json/custom-endpoint/v2/author/1我不知道你想要什么。您可以创建自定义类以向APICheck I install wp api插件添加新功能。当我输入地址栏wp json/v2/posts/122时,我得到了完整的帖子和所有帖子的详细信息。但如果我输入自定义的post id wp json/v2/posts/125,我得到了一个json数组,其中所有post detales,而不是data template=my template name.php。因此,只将模板名发送到浏览器,而不是数据,这是无用的。我想执行该文件并将输出发送到浏览器