admin管理员组文章数量:1334311
I am looking to use the rest API to use WordPress as a headless CMS, but I am curious as to how best to get a page using the path.
Example, if Wordpress is hosted at cms.example and my frontend is at example. If I create a page at cms.example/services/service-a I would want it accessible on the frontend at example/services/service-a. If my front end takes 'services/service-a' how can I find the right page?
My first thought was using the slug, until I realized the slug only matches the last part. In this example the slug would be 'service-a' not 'service\service-a'. I can specify a slug and a parent ID but this would require a a call for each level or hierarchy to work up the tree to get the parent id.
I have also briefly considered ignoring all previous levels of hierarchy and just looking at the last part of the path to use the slug, but I am not sure if that would have any unintended consequences. EDIT: Thinking about it more I am decidedly against this method as it is possible for two pages at different areas of hierarchy to have the same slug resulting in a different path.
I was thinking to setup a custom endpoint to take the path, run it through the get_page_by_path() function then return the id. But that means each page load would require 2 API calls, or at least each page load that isn't at the first level of hierarchy.
But wanting to limit each request to a single API call I am thinking either if possible add a custom argument to the pages API endpoint to specify a full path to search by for the response, or else if that is not explicitly possible creating my own endpoint to accept the path argument then return the matching page object. Obviously in this case my preference would be for the former over the latter as that should require less work for a plugin and make the most out of the existing code in WordPress.
Is it possible to add a custom option to an existing endpoint? Is there a better method to do this with a single API call?
I am looking to use the rest API to use WordPress as a headless CMS, but I am curious as to how best to get a page using the path.
Example, if Wordpress is hosted at cms.example and my frontend is at example. If I create a page at cms.example/services/service-a I would want it accessible on the frontend at example/services/service-a. If my front end takes 'services/service-a' how can I find the right page?
My first thought was using the slug, until I realized the slug only matches the last part. In this example the slug would be 'service-a' not 'service\service-a'. I can specify a slug and a parent ID but this would require a a call for each level or hierarchy to work up the tree to get the parent id.
I have also briefly considered ignoring all previous levels of hierarchy and just looking at the last part of the path to use the slug, but I am not sure if that would have any unintended consequences. EDIT: Thinking about it more I am decidedly against this method as it is possible for two pages at different areas of hierarchy to have the same slug resulting in a different path.
I was thinking to setup a custom endpoint to take the path, run it through the get_page_by_path() function then return the id. But that means each page load would require 2 API calls, or at least each page load that isn't at the first level of hierarchy.
But wanting to limit each request to a single API call I am thinking either if possible add a custom argument to the pages API endpoint to specify a full path to search by for the response, or else if that is not explicitly possible creating my own endpoint to accept the path argument then return the matching page object. Obviously in this case my preference would be for the former over the latter as that should require less work for a plugin and make the most out of the existing code in WordPress.
Is it possible to add a custom option to an existing endpoint? Is there a better method to do this with a single API call?
Share Improve this question edited Jun 6, 2020 at 4:04 user3452 asked Jun 6, 2020 at 1:46 user3452user3452 214 bronze badges2 Answers
Reset to default 1It is usually easier for me to decide how the front end should behave, and then determine how to get the appropriate data. So, in your example, services
would be a module with some functionality, and then service-a
would be the slug.
However, if you only care about the URL looking the same you can use pushState to change the URL in the browser without loading a new page. The "link" property in the posts API response could be parsed to get you the full path. As long as you get a unique identifier (e.g. slug) at the end, this would essentially make the middle of the path meaningless, but it could still be populated immediately after loading the page.
I found a filter hook "rest_page_query" and I was going to try to add a custom query var to the official pages endpoint, but while I could get the custom query var from my filter I could not figure out how to affect the output based on it.
However while looking at a custom endpoint I realized its super simple and was just easier to do that.
So I just put this code in a custom plugin:
add_action( 'rest_api_init', function () {
register_rest_route( 'custom/v1', '/pages', array(
'methods' => 'GET',
'callback' => 'my_page_from_path',
) );
} );
function my_page_from_path( $request ) {
$path = $request->get_param( 'path' );
$page = get_page_by_path( $path, ARRAY_A );
return rest_ensure_response( $page );
}
本文标签: wp apiWP Rest API get page from path
版权声明:本文标题:wp api - WP Rest API get page from path 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742369901a2462028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论