Wordpress 自定义页面模板和 WP-API
Wordpress custom page template and 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("content. php");
...
我的自定义页面 project_costs.php
执行以下步骤:
- 接收并设置用户从页面表单输入的变量 (POST/GET)。
- 从数据库中提取数据。
- 做一些计算和修改。
- 为用户创建页面。
我想将 angular.js 与 WP-API 插件集成。该插件只是从数据库中提取原始数据(第 2 步)并将其发送到前端(第 4 步)。因此没有重新加载未使用的页面和模板。
我想先将数据传递到我的 php class(第 3 步),然后将更改后的数据传递到 WP-API。
WP-API有没有调用我的PHP文件或函数的函数?
如有任何建议、示例或链接,我们将不胜感激。
谢谢。
所以我正在进行一个巨大的项目,其中包括几个 API/Angular #WordPress 的替换部件。一个文件是自定义端点-boilerplate.php。到目前为止,它的效果很好,但我们将不胜感激。
只需遵循结构并使用 my_awesome_function
到 return 做任何你想做的事。然后挂钩的命名空间和路由将可用,使用来自 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
我的 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 class(第 3 步),然后将更改后的数据传递到 WP-API。
WP-API有没有调用我的PHP文件或函数的函数?
如有任何建议、示例或链接,我们将不胜感激。
谢谢。
所以我正在进行一个巨大的项目,其中包括几个 API/Angular #WordPress 的替换部件。一个文件是自定义端点-boilerplate.php。到目前为止,它的效果很好,但我们将不胜感激。
只需遵循结构并使用 my_awesome_function
到 return 做任何你想做的事。然后挂钩的命名空间和路由将可用,使用来自 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