自定义网址WordPress REST API [英] Custom url WordPress REST API

查看:185
本文介绍了自定义网址WordPress REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用WordPress后端数据的应用程序.大多数数据都缓存在服务器上的JSON文件中,但是该应用程序允许放置注释,因此必须从应用程序内部调用该API.我担心当WordPress决定将URL从 /wp-json/wp/v2/ 更改为 /wp-json/wp/v3/ 时,我必须更新使用该URL的应用程序.这就是为什么我希望将此URL重写为更通用的名称,例如/api/.

I’m building an app which uses data from a WordPress backend. Most of the data is cached in JSON files on the server, but the app allows comments to be placed, so the API has to be called from within the app. I’m worried that when WordPress decides to change the URL from /wp-json/wp/v2/ to /wp-json/wp/v3/ I have to update the apps that use the URL. That is why I was hoping to rewrite this URL to something more generic like /api/.

这可能吗?我的第一次尝试失败了,只是显示了主题目录中的index.php:

Is this possible? My first attempt failed and simply shows the index.php from the theme directory:

RewriteRule ^api/(.*)$ /wp-json/wp/v2/$1 [NC,L]

推荐答案

您可以在wordpress主题函数中重写WordPress REST API网址.php

You can rewrite your WordPress REST API url in your wordpress theme function.php

function changeRestPrefix() {
return "wpjsoncustom"; //become yourwebsite/wpjsoncustom/wp/v2/
}
add_filter( 'rest_url_prefix', 'changeRestPrefix');

这篇关于自定义网址WordPress REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆