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

查看:38
本文介绍了自定义 url 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 主题 function.php 中重写你的 WordPress REST API url

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');

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

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