精华内容
下载资源
问答
  • WordPress REST API的许多重要用途之一就是改善您的插件或主题设置屏幕。添加自定义REST API端点后,通过AJAX获取保存的设置并通过AJAX(即无额外页面刷新)保存设置将变得更加简单。使用WordPress REST API代替admin-...

    WordPress REST API的许多重要用途之一就是改善您的插件或主题设置屏幕。添加自定义REST API端点后,通过AJAX获取保存的设置并通过AJAX(即无额外页面刷新)保存设置将变得更加简单。

    使用WordPress REST API代替admin-ajax不仅性能更高,而且还可以使WordPress核心在清理和验证方面完成大部分繁重的工作。

    在本文中,我们将逐步完成创建设置表单页面的每个步骤,并使用WordPress REST API处理该表单。

    添加设置页面

    在开始设计设置页面之前,我们需要向WordPress仪表板添加菜单或子菜单项,您可以在其中放置设置表单。在此页面上,您将需要加载CSS和JavaScript文件。

    这是一个入门类:

    在此类中,我使用add_menu_page创建一个顶层菜单,但是您可能希望根据需要使用add_sub_menu创建子菜单。

    这里有两件事要注意。

    首先是我们如何使用wp_localize_script()。每当加载第一个参数中指定的脚本时,此函数都可以为您提供一种使用PHP创建全局作用域JavaScript变量的方法。最初是为了向浏览器提供翻译后的本地化字符串而设计的。这就是我们使用它的一部分-提供可翻译的成功和错误消息。但是,它也可以用于传递动态值,例如当前站点的URL,在这种情况下为REST API终结点和随机数。我们将在JavaScript中需要所有这些功能,但是对于每个站点,它都是不同的,因此我们必须使用PHP即时生成它。

    另外,请注意,脚本的根URL作为对类的依赖关系而传入。我喜欢这样做,因为该URL可能会在插件或主题的其他地方使用,并且我希望在一个地方更改或过滤整个插件。

    实例化类时,需要指定该URL。这样做的一个好地方是在根插件文件中,使用plugin_dir_url()将生成正确的URL。让我们看一下设置它的主要插件文件:

    在此,我们使用“init”操作加载此类。我为REST API端点保留了一个占位符,一旦插件屏幕准备就绪,我们将使用它来保存数据。

    设置表单

    我不会对设置表单本身太深入,我可以为此写一整个系列。取而代之的是,我们仅添加两个字段来查看一些重要的内容。然后,我们可以编写JavaScript将表单值发送回服务器。

    这是“render_admin”方法,使用具有两个字段的字段进行了更新:

    我确保每个字段都有一个ID。这将允许我使用jQuery.val()定位每个对象以获取其值。这对于保持我们的HTML语义也很重要,因为字段标签的属性必须与字段ID相对应。我还给了表单一个ID,并添加了一个ID为“feedback”的空元素,我们可以在其中动态放置保存或错误消息。

    同样,您的表单可能会更复杂,但让我们开始吧。

    添加REST API路由

    在编写任何JavaScript通过AJAX将数据发送到服务器之前,我们需要为其提供REST API路由。我为此写了很多,但是值得通过一个非常简单的带有GET和POST端点的自定义路由。

    分离业务逻辑

    我坚信REST API路由的类应仅与处理请求有关,而与这些请求所需的业务逻辑无关。在这种情况下,“业务逻辑”是指读写实际设置。首先,让我们创建一个可以处理此问题的类。

    此类将是带有一些基本验证的get_option()和update_option()的简单包装。此类具有get_settings()方法,该方法获取保存的值,然后使用wp_parse_args()填充我们希望它具有的已保存数组的所有缺失索引。它还具有一个save_settings()方法,该方法确保只有要保存的数组的白名单键才在要保存的最终数组中。

    REST API路由

    现在,我们有了一种读写方法,可以通过任何方式寻址,让我们创建一个REST API路由,以充当其RESTful接口。该路由将具有GET和POST端点。

    如果您从未创建过自定义的REST API终结点,建议您阅读文档。我还介绍过与该主题相关的Torque文章和WordPress TV talks: 精选的链接列表  。

    这是REST API路由类:

    看一下,回调函数非常简单,因为它们只包装了我在上一节中创建的设置类。重要的是要了解,根据设计,设置类没有权限检查或清理操作。但是权限检查和清理非常重要。

    这些REST API端点提供了这一点。POST方法为每个字段指定一个“ sanitize_callback”参数。这样,我可以相信数据在传入之前是安全的。而且,这两个路由都使用“permissions_callback”,因此只能通过具有“ manage_options”功能的路由来访问这些路由。跳过这些步骤中的任何一个都是危险的。

    现在我们只需要在“rest_api_init”操作上实例化此类,以便存在端点。这又是主要的插件文件,对其进行了修改以实现此目的:

    /**

    * Plugin Name: Apex Plugin

    */

    add_action( 'init', function(){

    $assets_url = plugin_dir_url( __FILE__ );

    //Setup menu

    if( is_admin() ){

    new Apex_Menu( $assets_url );

    }

    //Setup REST API

    });

    在“设置”页面中使用REST API

    现在我们有了端点,让我们在设置页面上使用它们。我们将编写两个AJAX调用。第一个将获取保存的设置,并使用这些设置更新表单。这将由页面加载触发。第二个将由表单保存按钮触发,并将用于更新设置。

    在本教程的前面,我们告诉WordPress在此管理页面上加载JavaScript文件。现在该使用该文件了。

    这是第一个AJAX调用的样子。它的工作是获取保存的设置并使用以下内容更新表单:

    请注意此处的两个重要事项。首先,我使用之前通过wp_localize_script()设置的APEX全局对象来告诉jQuery请求什么URL。另外,我正在使用beforeSend()方法添加包含该对象中随机数的标头。否则,在API请求处理期间将不会认为用户已登录,因此我们的权限检查将失败。

    API请求完成后,使用jQuery.val()将设置添加到表单字段。为了安全起见,我使用Object.hasOwnProperty()确保它们在响应中。这是很重要的验证,但是随着设置数量的增长而无法很好地扩展-这是我使用VueJS进行这种事情的众多原因之一。

    现在,当您加载页面时,它应该获取已保存的设置(此时可能是默认值)并更新表单。很好,但是所有这些的真正意义在于能够更新设置。因此,我们将需要第二个AJAX调用,该调用将在提交表单时运行POST请求。

    这是带有保存AJAX调用的更新后的JavaScript:

    第二个调用非常相似,除了它使用POST并包装在与表单的commit事件绑定的闭包中。这样,它在表单提交时运行,我们可以防止该事件的默认操作发生。

    您应该在这里查看成功和错误方法。它们用于将文本(位于APEX.string对象中)本地化的消息添加到#feedback元素中。该对象中的“错误”消息非常笼统。但是大多数失败的请求都会生成一条带有消息的响应。因此,如果已设置,我们将改用它。

    自己去实践吧

    一旦有了一个良好的起点,就可以更新表单中的字段以符合您的需求。另外,您可能应该使用JavaScript框架来简化此过程,因为随着表单的复杂性增加,您将越来越难以使用jQuery进行管理并且会变得更加简单,并且如果您使用VueJS或React ,会提供更好的用户体验。

    本文向您展示了使用WordPress REST API 添加 WordPress设置页面的所有部分。我们添加了一个菜单页面,将我们的JavaScript和CSS入队,添加了一个用于读取和写入设置的类,添加了两个REST API端点作为这些设置的RESTful和安全接口,并使用jQuery AJAX根据我们的设置表单更新了设置。代码量很多,但我希望您已经了解了如何使用这些基础知识来改进自己的设置页面,或者从头开始构建自己的设置页面并从那里发展。

    拓展阅读:创建WordPress插件设置页面的5种方法

    展开全文
  • In this PR we are adding support for <a href="https://developer.wordpress.com/2016/11/11/wordpress-rest-api-on-wordpress-com/">WordPress REST API on WordPress.com</a> </p> <p><strong>To test: Since ...
  • Rest-Api-Wordpress-Vue2x
  • LuisBovoSandbox:React Redux Wordpress API REST
  • Learning WordPress REST API

    2016-12-03 22:02:35
    Learning WordPress REST API Learning WordPress REST API
  • 适用于JavaScript的WordPress REST API客户端 该库是的同构客户端,旨在与WordPress 5.0或更高版本一起使用。 如果您使用的是较旧的或WordPress 4.9,则某些命令将不起作用。 索引: 关于 node-wpapi是的同构...
  • "Testing via API via Python", "content":"tEST CONTENT OF THE THIS TEST PAGE via PYTHON", "status": "publish" } response = requests.post(url_srcdest, data=json.dumps(data), headers=headers)

    我能解决这个问题

    B.按照那里的帮助文本,为你的用户名创建密码字符串-假设它是mypassword123

    现在在你的终端上做这个

    "管理员:mypassword123“|基64

    您将获得一个新的密码-例如pwdabc123

    D.代码看起来像url_srcdest = "http://example.com/wp-json/wp/v2/pages/"

    headers = {'Content-Type': 'application/json',

    'Authorization': 'Basic pwdabc123',

    'Username': '',

    'Password':'pwdabc123'}

    data = \

    {

    "title":"Testing via API via Python",

    "content":"tEST CONTENT OF THE THIS TEST PAGE via PYTHON",

    "status": "publish"

    }

    response = requests.post(url_srcdest, data=json.dumps(data), headers=headers)

    展开全文
  • 将React SPA作为后端绑定到WordPress [带有WP REST API] 每个开发人员和他们的狗都有关于WordPress的话题,而且并不总是那么讨人喜欢。 离得很远。 但是,即使它最狂热的批评者也被迫注意到此功能,因为它已经改变...
  • wp rest api Before we discuss the WP REST API, it’s important to understand some terminology and background information. The acronym API stands for Application Programming Interface. An API is a ...

    wp rest api

    WP API WordPress plugin

    Before we discuss the WP REST API, it’s important to understand some terminology and background information. The acronym API stands for Application Programming Interface. An API is a programmatic way to interact with an application’s data. For example, Facebook’s API gives developers the ability to get all of the friends associated with a certain user. An API typically includes a specific set of instructions called documentation; to make it easy for any developer to work with it.

    在讨论WP REST API之前,了解一些术语和背景信息很重要。 缩写API代表应用程序编程接口。 API是与应用程序数据进行交互的编程方式。 例如,Facebook的API使开发人员能够获取与特定用户关联的所有朋友。 API通常包括一组特定的指令,称为文档。 使任何开发人员都可以轻松使用它。

    REST means Representational State Transfer. An API can be considered RESTful if its design/architecture subscribes to a specific set of constraints. You can look up what these constraints are here.

    REST表示代表性状态转移。 如果API的设计/体系结构订阅了一组特定的约束,则可以认为该API是RESTful的。 您可以在此处查找这些约束。

    HTTP requests are often the way that you interact with a RESTful API (HTTP requests are also the primary way that data is transmitted across the Internet). HTTP means Hyper Text Transfer Protocol. This protocol allows information to be shared between a client (cell phone, tablet, laptop, desktop, etc.) and a web accessible server in a request-response protocol. As an example, in order to publish a status to a user’s Facebook timeline, a HTTP request targeting that action on behalf of that user would be sent from our JavaScript code to Facebook’s API (ie. a Facebook server). The client (JavaScript code) would receive a response from the Facebook server indicating that the user’s status was successfully published.

    HTTP请求通常是您与RESTful API交互的方式(HTTP请求也是通过Internet传输数据的主要方式)。 HTTP表示超文本传输​​协议。 该协议允许以请求-响应协议在客户端(手机,平板电脑,笔记本电脑,台式机等)和Web可访问服务器之间共享信息。 例如,为了将状态发布到用户的Facebook时间轴,将代表该用户的针对该操作的HTTP请求从我们JavaScript代码发送到Facebook的API(即Facebook服务器)。 客户端(JavaScript代码)将从Facebook服务器收到响应,指示用户状态已成功发布。

    HTTP Request Flow

    1. A client makes a HTTP request to a server and 2. The server responds with an HTTP response.

    1.客户端向服务器发出HTTP请求,并且2.服务器以HTTP响应进行响应。

    In a HTTP request, you need to define the type of action that you want to perform against a resource. There are four primary actions associated with any HTTP request (commonly referred to as CRUD):

    在HTTP请求中,您需要定义要对资源执行的操作类型。 与任何HTTP请求(通常称为CRUD)相关联的主要动作有四个:

    1. POST (Create)

      POST(创建)
    2. GET (Retrieve)

      GET(检索)
    3. PUT (Update)

      PUT(更新)
    4. DELETE (Delete)

      删除(删除)

    A resource is a data object that can be accessed via a HTTP request. The WP REST API allows you to “access your WordPress site’s data (resources) through an easy-to-use HTTP REST API”. In the case of the most recent version of the WP API (version 2), the resources include the following 9 WordPress objects:

    资源是可以通过HTTP请求访问的数据对象。 WP REST API允许您“通过易于使用的HTTP REST API访问WordPress站点的数据(资源)”。 对于最新版本的WP API(版本2),资源包括以下9个WordPress对象:

    1. Posts

      帖子
    2. Pages

      页数
    3. Media

      媒体
    4. Post meta

      发布元
    5. Post revisions

      发布修订
    6. Comments

      注释
    7. Taxonomies

      分类法
    8. Terms

      条款
    9. Users

      用户数

    With the WP API, you can perform any of the four CRUD actions against any of your WordPress site’s resources listed above. For example, you can use the WP API to create a post, retrieve a post, update a post or delete a post associated with your WordPress website.

    使用WP API,您可以对上面列出的WordPress网站的任何资源执行四个CRUD操作中的任何一个。 例如,您可以使用WP API创建帖子,检索帖子,更新帖子或删除与WordPress网站关联的帖子。

    It’s important to know that some HTTP requests don’t require authentication (anyone can make requests and the corresponding responses). In WP API, for example, certain GET requests like getting posts and getting media don’t require authentication. Other GET requests do require authentication though. For example, getting post revisions, getting users and getting post meta data require authentication. In addition, all POST, PUT and DELETE WP API actions require authentication.

    重要的是要知道某些HTTP请求不需要身份验证(任何人都可以发出请求和相应的响应)。 例如,在WP API中,某些GET请求(例如获取帖子和获取媒体)不需要身份验证。 其他GET请求确实需要身份验证。 例如,获取帖子修订,获取用户和获取元数据都需要身份验证。 此外,所有POST,PUT和DELETE WP API操作都需要身份验证。

    In WP API’s case, authentication for on site actions (plugins or themes installed to the site) are handled by cookies. However, if you’re off site, authentication is handled by OAuth (You would need to download the OAuth plugin to your WordPress site and then any external site would need to go through the normal OAuth flow to gain access. There’s also a Basic Authentication plugin that you could use as well).

    在WP API的情况下,站点操作(安装到站点的插件或主题)的身份验证由cookie处理。 但是,如果您不在站点,则身份验证由OAuth处理(您需要将OAuth插件下载到WordPress站点,然后任何外部站点都需要通过常规OAuth流程来获得访问权限。还有基本身份验证您也可以使用的插件)。

    Creating, retrieving, updating or deleting WordPress site data “is as simple as sending a HTTP request.”

    创建,检索,更新或删除WordPress网站数据“就像发送HTTP请求一样简单。”

    Now that you hopefully have a better understanding of what the WP API is, we’ll explore possible use cases as well as the APIs extensibility, limitations, installation instructions and making a couple example API calls.

    现在,您希望对WP API有了更好的了解,我们将探讨可能的用例以及API的可扩展性,局限性,安装说明并进行一些API调用示例。

    WP API用例 (Use Cases for the WP API)

    In a presentation at WordCamp San Francisco in 2014, Sam Hotchkiss said that the WP API could result in “plugins running solely against the REST API; without having to have any PHP installed on that site’s server.” He thinks that the WP API may be opening up the door “for a third party app store” for plugins.

    在2014年旧金山WordCamp的一次演讲中,Sam Hotchkiss表示WP API可能会导致“插件仅针对REST API运行; 无需在该站点的服务器上安装任何PHP。” 他认为WP API可能为插件的“第三方应用商店”打开大门。

    On a May 27, 2015 WordPress Weekly episode, Matt Mullenweg, one of the co-founders of WordPress, said that the WP REST API is going to be “huge and revolutionary for developers”. Developers will be able to “build applications against it in a decoupled way”.

    WordPress的共同创始人之一Matt Mullenweg在2015年5月27日的WordPress周刊中表示,WP REST API将“对开发人员来说是巨大而革命性的”。 开发人员将能够“以分离的方式针对它构建应用程序”。

    Matt made sure to emphasize that “it really gets amazing when it’s combined with something else. Imagine a future version of HappyTables… where they don’t modify WP admin at all. It just creates a custom interface that talks purely over the REST API. Instead of trying to hack everything in WP admin.”

    马特(Matt)一定要强调:“当它与其他东西结合在一起时,它真的变得很棒。 想象一下HappyTables的未来版本……他们根本不修改WP admin。 它只是创建一个纯粹通过REST API进行通信的自定义接口。 而不是尝试破解WP管理员中的所有内容。”

    Matt continued, saying “I think [the] REST API is the beginning of WP admin becoming just one of many clients for managing WordPress. Think of WP admin as PHP, plus HTML, plus JavaScript clients. I think that you will see native clients. I think you’ll see pure JavaScript clients. I think you’ll see other PHP, HTML, JS clients; perhaps in the [vein] of HappyTables or some other verticals that have sprung up around WordPress”. He thinks that it may “make the admin [dashboard] something that you can replace as easily as you can replace the themes.”

    马特继续说:“我认为[REST API]是WP管理员的开始,成为众多管理WordPress的客户端之一。 将WP admin视为PHP,HTML和JavaScript客户端。 我认为您会看到本地客户。 我认为您会看到纯JavaScript客户端。 我想您还会看到其他PHP,HTML,JS客户端; 也许是在HappyTables的[静脉]或其他围绕WordPress兴起的垂直领域中”。 他认为这可能“使管理员[dashboard]变得可以像替换主题一样容易地替换。”

    高度可扩展 (Highly Extensible)

    The WP API’s ability to access and manipulate a WordPress site’s resources remotely is cool enough in and of itself, but its extensibility makes it even more exciting. The WP API provides ways to both add additional fields to resources and to add endpoints to the API.

    WP API远程访问和操纵WordPress网站资源的能力本身本身就很酷,但是它的可扩展性使其更加令人兴奋。 WP API提供了将附加字段添加到资源以及将端点添加到API的方法。

    向资源添加字段 (Adding Fields to Resources)

    Using the register_api_field function, you can add additional fields to be updated or retrieved when you make requests to certain WP API resources. You first need to run the add_action function and use rest_api_init as the hook (you would likely add this code via a WordPress plugin). The second parameter is the function name that is run when the hook is encountered. The function name in this instance is register_post_custom_field. custom_field is the field name, post is the resource and get_custom_field is the callback function that is run when a GET request is called on the post resource.

    使用register_api_field函数,可以向请求某些WP API资源时添加要更新或检索的其他字段。 首先,您需要运行add_action函数并将rest_api_init用作挂钩(您可能会通过WordPress插件添加此代码)。 第二个参数是遇到钩子时运行的函数名称。 在此实例中,函数名称为register_post_custom_field。 custom_field是字段名称,post是资源,而get_custom_field是在发布资源上调用GET请求时运行的回调函数。

    function register_post_custom_field() {
            register_api_field( 'post',
                'custom_field',
                array(
                    'get_callback'    => 'get_custom_field',
                    'update_callback' => null,
                    'schema'          => null,
                )
            );
        }

    You then need to add the get_custom_field callback function to the same file. In this function (as you can see below), you get the value of the “custom_field” for the post resource and you return it. This is so that when a call is made to get the custom field value, you will receive the custom field value in response.

    然后,您需要将get_custom_field回调函数添加到同一文件。 在此函数中(如下所示),您将获得帖子资源的“ custom_field”值,然后将其返回。 这样一来,当调用获取自定义字段值时,您将收到自定义字段值作为响应。

    function get_custom_field( $object, $field_name, $request ) {
            return get_post_meta( $object[ 'id' ], $field_name, true );
        }

    添加端点 (Adding Endpoints)

    According to wp-api.org, “endpoints are functions available through the API. This can be things like updating a post or deleting a comment. Endpoints perform a specific function, taking some number of parameters and returning data to the client. A route is the “name” you use to access endpoints, used in the URL. A route can have multiple endpoints associated with it, and which is used depends on the HTTP verb”.

    根据wp-api.org,“端点是可通过API使用的函数。 这可能是更新帖子或删除评论之类的事情。 端点执行特定的功能,需要一些参数并将数据返回给客户端。 路由是URL中用于访问端点的“名称”。 路由可以具有与之关联的多个端点,其使用取决于HTTP动词。

    In addition to adding fields to default resources, you can register custom endpoints to be used with the WP API. In order to register custom endpoints, you need to call the add_action function and use the rest_api_init hook and the register_rest_route function (again you’d likely add this code in a WordPress plugin). In the example below, the author/{author_id} route is registered and the GET endpoint for that route is defined (the callback function is get_post_title_by_author).

    除了将字段添加到默认资源之外,您还可以注册要与WP API一起使用的自定义端点。 为了注册自定义端点,您需要调用add_action函数并使用rest_api_init钩子和register_rest_route函数(同样,您可能会在WordPress插件中添加此代码)。 在下面的示例中,作者/ {author_id}路由已注册,并定义了该路由的GET端点(回调函数为get_post_title_by_author)。

    add_action( 'rest_api_init', function () {
                register_rest_route( 'myplugin/v1', '/author/(?P\d+)', array(
                    'methods' => 'GET',
                    'callback' => 'get_post_title_by_author'
                ) );
            } );

    The following is the callback function that gets called when the GET endpoint is accessed via the WP API. FYI, posts are retrieved by the post’s author id in this function and the latest post title is returned.

    以下是通过WP API访问GET端点时调用的回调函数。 仅供参考,通过此功能中帖子的作者ID检索帖子,并返回最新的帖子标题。

    function get_post_title_by_author( $data ) {
                $posts = get_posts( array(
                    'author' => $data['id'],
                ) );
            
                if ( empty( $posts ) ) {
                    return null;
                }
            
                return $posts[0]->post_title;
            }

    The ability to add endpoints and fields to the default WP API functionality makes it easier for developers to use the WP API on many different types of WordPress sites and provides more iterative opportunities in the future.

    将端点和字段添加到默认WP API功能的功能使开发人员可以更轻松地在许多不同类型的WordPress网站上使用WP API,并在将来提供更多的迭代机会。

    局限性 (Limitations)

    There are some important limitations that you need to keep in mind before you begin working with the WP API. First off, any form of Serialized meta data is not allowed to be read or stored using the WP API. The creators of the API say that this is because “JSON can’t hold all formats of data stored in PHP” (for example, custom PHP objects cannot be represented), serialized data could also expose private data and serialized data has security problems (remote code execution vulnerabilities in particular).

    在开始使用WP API之前,需要记住一些重要的限制。 首先,不允许使用WP API读取或存储任何形式的序列化元数据。 API的创建者说这是因为“ JSON不能保存PHP中存储的所有格式的数据”(例如,无法表示自定义PHP对象),序列化的数据也可能暴露私有数据,而序列化的数据存在安全性问题(远程代码执行漏洞)。

    In addition, protected meta cannot be accessed or saved via the WP API. A protected meta is any meta field that has a key that begins with the _ (underscore) character. These meta fields cannot be exposed via the API.

    此外,无法通过WP API访问或保存受保护的元。 受保护的元数据是任何具有以_(下划线)字符开头的键的元字段。 这些元字段无法通过API公开。

    All other meta data is only available when “authenticated with permission to edit the post that the meta is attached to”. This is because any user can enter meta values via the Custom Fields metabox and they want to protect user privacy.

    所有其他元数据仅在“已获得编辑元数据所附加的帖子的权限而获得认证”时才可用。 这是因为任何用户都可以通过“自定义字段”元框输入元值,并且他们想保护用户隐私。

    It’s also important to note that the current version two beta “does not guarantee forwards compatibility with future betas.” They continue saying that “while we believe the API is now stable enough for public testing, we may continue to break the API in the future as we improve it further. Only use the API in development, and do not use version 2 in production environments.” You could use version 1 of the WP API, but we would recommend sticking to version 2 and just waiting to use it in production until they say it’s ready for production.

    还需要注意的是,当前的两个Beta版本“不保证与将来的Beta版本具有向前兼容性。” 他们继续说:“尽管我们认为API现在已经足够稳定,可以进行公开测试,但我们可能会在将来进一步改进该API的同时继续破坏该API。 仅在开发中使用API​​,在生产环境中不使用版本2。” 您可以使用WP API的版本1,但我们建议您坚持使用版本2,然后等待生产中使用它,直到他们说它已准备好投入生产为止。

    Lastly, in the same WordPress Weekly episode mentioned previously (on May 27), Matt Mullenweg talked about the limitations of the WP REST API’s authentication flow. He said that “third party integrations out of the box will still not be as smooth as you might be used to, like logging in with Facebook or logging in with Twitter to integrate something. That will still be a multi step process or require the site’s owner to go through some extra steps.”

    最后,在之前提到的同一本WordPress周刊(5月27日)中,Matt Mullenweg谈到了WP REST API身份验证流程的局限性。 他说:“开箱即用的第三方集成仍然不会像您以前所习惯的那样平滑,例如使用Facebook登录或使用Twitter登录以集成某些内容。 这仍然是一个多步骤的过程,或者要求站点的所有者执行一些额外的步骤。”

    安装说明 (Installation Instructions)

    You should have a solid idea of what the WP API is capable of and what its limitations are. Let’s show you the steps involved in working with it. First off, you need to add the WP API plugin to your WordPress site.

    您应该对WP API的功能及其局限性有扎实的认识。 让我们向您展示使用它的步骤。 首先,您需要将WP API插件添加到WordPress网站。

    Go to https://wordpress.org/plugins/rest-api. Click the red Download button. This should download the latest version of the WP API plugin as a zip file.

    转到https://wordpress.org/plugins/rest-api。 单击红色的下载按钮。 这应该以zip文件的形式下载WP API插件的最新版本。

    WP REST API WordPress plugin download

    Then, login to your WordPress site (your-site-name.com/wp-login.php). Hover over plugins in the left sidebar and click on Add New. Click on the Upload Plugin button, click Choose File and select the compressed version of the WP API plugin and then click Install Now.

    然后,登录到您的WordPress网站(your-site-name.com/wp-login.php)。 将鼠标悬停在左侧边栏中的插件上,然后单击添加。 单击上载插件按钮,单击选择文件并选择WP API插件的压缩版本,然后单击立即安装。

    WP API WordPress Plugin Upload and Install

    Now that it’s installed, click the ‘Activate Plugin’ link.

    安装完成后,点击“激活插件”链接。

    WP API WordPress Plugin Activate

    Change permalinks to something other than the default; we set ours to the Post name option.

    将永久链接更改为默认值以外的其他值; 我们将我们的设置为“帖子名称”选项。

    WordPress Permalink Settings

    That’s everything that’s involved with configuring the WP REST API. Now, we can get to the fun stuff and get some data from your WordPress website using the WP API.

    这就是配置WP REST API所涉及的一切。 现在,我们可以使用WP API来获取有趣的东西并从您的WordPress网站获取一些数据。

    WP REST API请求示例 (Example WP REST API Requests)

    We’re going to show you examples of some unauthenticated WP API GET requests. So, there’s no need to worry about any authentication plugins or settings.

    我们将向您展示一些未经身份验证的WP API GET请求的示例。 因此,无需担心任何身份验证插件或设置。

    Start off by adding the Postman Chrome Extension. You could also install the REST Easy Add-On if you use Firefox.

    首先添加Postman Chrome扩展程序 。 如果使用Firefox,也可以安装REST Easy Add-On

    获取帖子 (Getting Posts)

    We’re going to show you how to get all of the posts that currently exist on your WordPress site. In order to do this, copy the following URL (route):

    我们将向您展示如何获取WordPress网站上当前存在的所有帖子。 为此,请复制以下URL(路由):

    http://www.your-web-site.com/wp-json/wp/v2/posts

    Paste that route into Postman’s Enter request URL here field (You could also do this by simply typing the route above in a web browser but the data won’t be formatted (so it’ll be more difficult to see the schema)). Replace ‘your-web-site.com’ with the website that you installed the WP API plugin on (in our case it was the website wpmerchant.com; as you can see from the image below). Select GET from the drop down and click ‘Send’.

    将该路由粘贴到Postman的“在此处输入请求URL”字段中(您也可以通过在网络浏览器中简单地在上面键入路由来完成此操作,但是数据不会被格式化(因此,很难看到架构))。 将“ your-web-site.com”替换为安装了WP API插件的网站(在我们的示例中是网站wpmerchant.com;从下图可以看到)。 从下拉列表中选择GET,然后单击“发送”。

    Get Posts using the WP API

    This is sending a GET request to your WordPress site’s server and the server is sending back a response based on the route and the HTTP action that is set. You should see a JSON response similar to the one below.

    这会将GET请求发送到您的WordPress站点的服务器,并且服务器将根据路由和所设置的HTTP操作发送回响应。 您应该看到类似于以下内容的JSON响应。

    Get Posts WP API Plugin Response

    As you can see, this response is structured in a certain way so that you can easily and predictably access the data within it; this is called a schema. This structure is really important because knowing it, allows you to sift through the data programmatically. The response starts with a [ (left square bracket). This identifies the response as a JSON array. If you look at the response in more detail, you can find, amongst other information, that the first post in the array has a post ID of 838, a post title of WPMerchant: Simple and Powerful eCommerce for WordPress as well as the post content starting with <p>Powerful features in a simple interface.). This structure is carried out throughout all of the other posts that are returned in the response.

    如您所见,此响应是以某种方式构造的,因此您可以轻松,可预测地访问其中的数据。 这称为架构。 这种结构非常重要,因为了解它可以让您以编程方式筛选数据。 响应以[((左方括号)开头。 这将响应标识为JSON数组。 如果您更详细地查看响应,则除其他信息外,您还可以发现数组中的第一条帖子的帖子ID为838WPMerchant的帖子标题:WordPress的简单而强大的电子商务以及该帖子的内容从<p>简单界面中的强大功能开始 )。 此结构在响应中返回的所有其他所有帖子中执行。

    获取帖子 (Get a Post)

    Now that you know how to get a list of the posts on your site, we’ll show you how to get a specific post from your WordPress site with the WP API plugin installed. Copy the route below.

    现在您知道了如何获取网站上的帖子列表,我们将向您展示如何从安装了WP API插件的WordPress网站获取特定帖子。 复制以下路线。

    http://www.your-web-site.com/wp-json/wp/v2/posts/{id}

    Paste that into the Postman Enter request URL here field. Again, make sure you select GET from the drop down. Replace your-web-site.com with the website that you installed the WP API plugin on AND replace {id} with a Post ID that you know exists on the WordPress site.

    将其粘贴到“邮递员在此处输入请求URL”字段中 。 同样,请确保从下拉列表中选择GET。 将您的web-site.com替换为您在WP API插件上安装的网站,并将{id}替换为您知道WordPress网站上存在的帖子ID。

    Get a Post using the WP API Plugin

    Click on the blue Send button. You should see something similar to the following response:

    单击蓝色的发送按钮。 您应该看到类似于以下响应的内容:

    Get a Post Response using the WP API Plugin

    As you can see, there is no left square bracket to start off the response. This means that this response is an object NOT an array of objects. Specifically, this response includes all of the post data related to the post with a post ID of 838. The individual post data is in the same format as the list of posts above. You can again parse through the response and see the post id, post title, post content, post excerpt and many other post details.

    如您所见,没有左方括号开始响应。 这意味着此响应是一个对象,而不是对象数组。 具体来说,此响应包含与帖子相关的所有帖子数据,其帖子ID为838。各个帖子数据的格式与上述帖子列表相同。 您可以再次解析响应,并查看帖子ID,帖子标题,帖子内容,帖子摘录和许多其他帖子详细信息。

    Have fun and test out some other unauthenticated HTTP requests!

    玩得开心,测试其他未经身份验证的HTTP请求!

    结论 (Conclusion)

    As you can tell, we’re really excited about using the WP API as well as the the implications of the WP REST API and the different functionality that will result from its use (and hopefully eventual incorporation into the WordPress core).

    如您所知,我们对使用WP API以及WP REST API的含义以及使用它会带来的不同功能感到非常兴奋(并希望最终并入WordPress核心)。

    In a subsequent article, we’re going to create an external site that creates, retrieves, updates and deletes WordPress posts using the WP REST API. So, check out our Twitter feed to stay tuned!

    在后续文章中,我们将创建一个外部站点,该站点使用WP REST API创建,检索,更新和删除WordPress帖子。 因此,请查看我们的Twitter供稿以保持关注!

    继续对话 (Continue the Conversation)

    How do you plan on using the WP API? How do you foresee it being used? We would love to hear your thoughts below!

    您如何计划使用WP API? 您如何预见它的使用? 我们很乐意听到您在下面的想法!

    翻译自: https://www.sitepoint.com/wp-api/

    wp rest api

    展开全文
  • <p>I tried finding it here <a href="https://developer.wordpress.com/docs/api/" rel="nofollow">https://developer.wordpress.com/docs/api/</a> but no luck.</p> <p>So please suggest. </div>
  • REST API工具箱 贡献者: gungeekatx 标签:休息api,休息,wp休息api,json api 捐赠链接: : 至少需要: 4.4 经测试至: 4.9 稳定标签: 1.4.2 许可证: GPLv2或更高版本许可URI: : 允许调整几种REST API设置 ...
  • This article on developing a WordPress REST API app was originally published by Torque Magazine, and is reproduced here with permission. 这篇有关开发WordPress REST API应用程序的文章最初由Torque ...

    wordpress api

    This article on developing a WordPress REST API app was originally published by Torque Magazine, and is reproduced here with permission.

    这篇有关开发WordPress REST API应用程序的文章最初由Torque Magazine发行 ,并经许可在此处转载。

    Excitement over the WordPress REST API has been building for what seems like an eternity, but we’re still pretty much at the starting gate in terms of what it’s actually going to mean for site owners and developers once it finally lands.

    对WordPress REST API的兴奋一直在建立似乎是永恒的事物,但是一旦它最终登陆,对于站点所有者和开发人员实际上意味着什么,我们仍处于起步阶段。

    Though I’ve written extensively about the potential implications of the REST API, I’ve been holding back on actually diving in and getting my hands dirty to date. With the REST API finally taxiing on the runway, now seemed a great time to grasp that nettle and really start digging into detail.

    尽管我已经撰写了大量有关REST API 潜在影响的文章 ,但我一直坚持不懈地进行深入研究,直到现在为止。 随着REST API终于在跑道上滑行了 ,现在似乎是抓住这一荨麻并真正开始深入研究细节的好时机。

    Over the course of this series, I’ll be taking the new hotness for a spin by putting together a simple JavaScript app that uses the REST API to power its content. It promises to be an intense learning experience, but one that will hopefully serve others who are coming from a non-technical background as well.

    在本系列的整个过程中,我将通过组装一个使用REST API为其内容提供动力的简单JavaScript应用程序,来探索新的热点。 它有望成为一种丰富的学习经历,但也有望为其他非技术背景的人服务。

    我的背景简介 (A Brief Note on My Background)

    Though WordPress has long been a passion of mine, I’m a writer and entrepreneur by trade. I can’t pretend to be coming at this project from any type of serious development background. “Knows just about enough to be dangerous” would be how I’d charitably classify my coding experience to date.

    尽管WordPress长期以来一直是我的爱好,但我还是一名作家和企业家。 我不能假装从任何类型的认真开发背景来从事这个项目。 到目前为止,我将如何对自己的编码经验进行慈善归类,“知道足够危险”。

    So, this series won’t be quite the deep dive you might expect from a theming professional such as Jack Lenox, or a senior web developer such as Ramsay Lanier. It should also be no great surprise that I’m not approaching this from the point of view of an established top-tier digital agency looking to kick the tires of the latest technology.

    因此,本系列不会像杰克·莱诺克斯(Jack Lenox)这样的主题专业人士或Ramsay Lanier这样的高级Web开发人员那样深入。 从一家成熟的顶级数字代理机构寻求采用最新技术的角度来看,我没有采用这种方法也就不足为奇了。

    Major agencies such as Modern Tribe are already working with the REST APIMajor agencies such as 诸如
    Modern Tribe are already Modern Tribe之类的主要机构已经working with the REST API 在使用REST API

    Nope, this series will be much more along the lines of an average WordPress user looking to get to grips with the next generation of the platform via a practical, exploratory project.

    不,这个系列的内容将更多地与普通WordPress用户相似,他们希望通过一个实际的探索性项目来掌握下一代平台。

    Hopefully that’s an approach that will also resonate with a significant percentage of other site owners out there. Put it this way – if your eyes instantly start to glaze over at the introductions of other (admittedly excellent) tutorials such as the one below, this is the series for you:

    希望这是一种方法,也可以引起很多其他网站所有者的共鸣。 这样说吧–如果您的眼睛立即开始凝视其他(公认的出色)教程的介绍,例如下面的教程,那么本系列适合您:

    In the following weeks, through a series of articles, I’ll explain how I’m using Node.js and Express on the backend with a GraphQL server hooked up to a MYSQL WordPress database that uses Apollo to fetch data and pipe it into React components. Don’t worry, I’ll still be using the tried and true WordPress admin interface.

    在接下来的几周中,通过一系列文章,我将说明如何在GraphQL服务器上使用Node.js和Express并将其连接到使用Apollo来获取数据并将其通过管道传输到React的MYSQL WordPress数据库组件。 别担心,我仍然会使用久经考验的WordPress管理员界面。

    With those caveats out of the way, let’s briefly recap why now is a great time to be really rolling up your sleeves and using the REST API in earnest.

    消除了这些警告,让我们简要地回顾一下为什么现在是真正振作精神并认真使用REST API的好时机。

    为什么现在是时候拥抱REST API (Why Now Is the Time to Embrace the REST API)

    The launch of Calypso and Matt Mullenweg’s State of the Word address back in late 2015 made it crystal clear which way the wind is blowing in the world of WordPress at large. To put it in a nutshell, the REST API is going to be at the centre of the next stage of the platform’s future, and developers are going to have to get on board with JavaScript sooner rather than later.

    早在2015年末,Calypso和Matt Mattlenweg的Word状态报告的发布就使人们很清楚地知道WordPress世界中的风向。 简而言之,REST API将成为平台未来下一阶段的中心,并且开发人员将必须早日使用JavaScript。

    The arrival of Calypso points the way to the future of WordPress

    We’ve already seen entire conferences devoted to exploring the implications of the REST API, and increasingly large real-world projects basing themselves around it, despite its late arrival. From Microsoft to the New York Times, blue-chip companies worldwide are chomping at the bit to really explore its power.

    我们已经看到整个会议都致力于探讨REST API的含义,尽管它来迟了,但越来越多的大型现实项目围绕它而建立。 从微软到《纽约时报》 ,全世界的蓝筹公司都在争先恐后地探索其力量。

    If you’re a theme or plugin developer, you can rest assured that the vast majority of your competition are already, at the very least, actively researching the topic. If you’re a site owner, you can expect the next five years or more of your site’s development to be significantly defined by the possibilities that the REST API opens up. No matter what way you look at it, now is the time to get on board this particular train.

    如果您是主题或插件开发人员,则可以放心,您竞争的绝大部分已经至少正在积极地研究该主题。 如果您是网站所有者,则可以通过打开REST API的可能性对网站的未来五年或更长的发展进行重大定义。 无论您如何看待它,现在都是时候登上这列特别的火车。

    我们将在本系列中做什么 (What We’ll Be Looking to Do in This Series)

    In this series, we’re going to start from scratch with a local install, and use WordPress to house a collection of quotes from a great American original — Mr. Henry David Thoreau. With our words of wisdom safely stored in the WordPress back end, we’ll be looking at interacting with them via the REST API, and building out a simple JavaScript-powered front end to display them in a variety of ways using Facebook’s React library.

    在本系列中,我们将从本地安装开始,从头开始,并使用WordPress来存放美国原著-Henry David Thoreau先生的引文集。 我们将智慧的话语安全地存储在WordPress后端中,我们将着眼于通过REST API与之进行交互,并构建一个简单JavaScript驱动的前端,以使用Facebook的React库以多种方式显示它们。

    We’ll be using the current plugin version of the REST API

    Along the way, we’ll touch on subjects such as alternative front end solutions, integration with mobile apps, design tips and tweaks, and experimenting with third-party APIs for added functionality. By the time we’re finished, you should have a much more grounded and practical view of what the REST API is actually all about.

    在此过程中,我们将涉及诸如替代前端解决方案,与移动应用程序集成,设计技巧和调整以及尝试使用第三方API以获得附加功能等主题。 待完成时,您应该对REST API的实质有了更扎实和实用的了解。

    We’ll be using WordPress 4.5.3 running on a local development environment, version 2 of the REST API as a plugin, React, and a whole lot of patience and persistence to put together our finished project. Stick along for the ride and you’re sure to pick up a ton of useful info along the way!

    我们将使用在本地开发环境上运行的WordPress 4.5.3 ,作为插件的REST API版本2React和大量的耐心和持久性来组合完成的项目。 坚持前进,您一定会在途中获得大量有用的信息!

    结论 (Conclusion)

    The tools to get cracking with the REST API already exist, it’s used in production by several major sites worldwide, and it won’t be long until it hits the mainstream WordPress world in earnest. There’s simply never been a better time to learn about it.

    可以使用REST API进行破解的工具已经存在,并且已在全球多个主要站点用于生产中,并且很快就将其打入主流WordPress世界不久。 从来没有比现在更好的时间来学习它。

    Aimed at a relatively non-technical audience, our series on developing a REST API app from scratch will take you from zero to hero in no time at all. You won’t need a computer science degree to follow along – just a little time, patience and perseverance.

    针对相对非技术性的读者,我们的系列文章从头开始开发REST API应用程序,将使您从零变成英雄。 您不需要计算机科学学位就可以继续学习–只需一点时间,耐心和毅力。

    Stay tuned for part two, in which we’ll fire up our local site, get some data into it, and start looking at basic reading and writing functionality via the REST API.

    请继续关注第二部分,在第二部分中,我们将启动本地站点,获取一些数据,然后开始研究通过REST API进行的基本读写功能。

    You can read the other articles in this series here.

    您可以在这里阅读本系列的其他文章。

    翻译自: https://www.sitepoint.com/developing-a-wordpress-rest-api-app/

    wordpress api

    展开全文
  • -了解Next.js功能和API。 交互式Next.js教程。 您可以查看-欢迎您提供反馈和意见! 在Vercel上部署 部署Next.js应用程序的最简单方法是使用Next.js创建者提供的。 请查看我们的以获取更多详细信息。
  • Blog-API-REST-Wordpress-React-JS
  • WordPress Rest API 入门详解

    万次阅读 2019-02-22 17:19:46
    一、关于WP REST API wordpress已经不仅仅是一个博客...WP REST APIwordpress的一个插件,为wordpress提供对外标准的RESTFul接口,随着RESTFul的畅行和各种前端框架的出现,WP REST API可以让wordpress不仅仅...
  • Vue.js 是用于构建交互式的 Web 界面的库。它提供了 MVVM 数据绑定和一个可组合的组件系统,具有简单、...WordPress REST APIWordPress数据类型提供API端点,允许开发人员通过发送和接收JSON(JavaScript Object No...
  • WordPress Rest API列表 详解

    千次阅读 2019-05-09 17:20:34
    ??????????...个人博客:Harvey的个人博客 ?...一、关于WP REST API ...wordpress已经不仅仅是一个博客...WP REST APIwordpress的一个插件,为wordpress提供对外标准的RESTFul接口,随着RESTFul的畅行和各种前端框架的出现...
  • WordPress 说明: 本项目为小程序入门项目,需要一定的前端基础及一点后端知识,项目可能不太好,欢迎大家吐槽,纯萌新小白勿入,最后祝大家Coded愉快。 使用方法: git clone ...
  • WordPress REST API Support

    2020-12-31 16:10:36
    <div><p>What sort of functionality would be useful for inclusion in Sensei to support the REST API? <p>Possibilities: Administrative tasks such as viewing student progression, quiz results, and such. ...
  • go-wordpress, Golang API的客户端库( Wordpress REST API ) go-wp-apiGolang api的客户端库( Wordpress REST API )安装go get github.com/sogko/go-wordpress用法快速示例package main
  • 全球使用量最大的网站系统 wordpress大概是从4.5内置了 wordpress rest api的功能,使 wordpress与其他系统进行集成变得非常方便,这套接口几乎涵盖了日常用到的所有功能。需求说回正题,最近上公众号后台发现经常有...
  • <p>I am now creating an android app which planned to connect to a plugin(WP Job Manager) in my WordPress side....I am new in rest-api and I am not sure what I should do. <p>Thankyou! </div>
  • 基于React.js和WordPress REST APIWordPress渐进式Web应用程序主题 产品特点 应用程序外壳:导航栏和简单的页脚 帖子索引 单发 单页 简单(无侧边栏,无元数据) 入门 在开始之前,请确保已在系统上全局安装了...
  • jp-rest-cache, 用于 WordPress REST API的高速缓存( API ) 缓存软过期,服务器端缓存用于 WordPress REST API ( WP REST ) 。利用jaquith库的标记。 需要WordPressWordPress REST API 。请注意使用时请注意两种...
  • WordPress JSON REST API

    2020-08-30 19:03:58
    WordPress has a big market share of the web. REST APIs are a growing technique and a big opportunity for developers. Knowing how to create APIs and how to consume them gives you a great advantage. A R...
  • REST API到ACF 在公开端点 安装 将acf-to-rest-api文件夹复制到wp-content/plugins文件夹中 通过插件管理页面激活ACF to REST API插件 终点 终点 可读的 可编辑的 / wp-json / acf / v3 / posts / wp-json / acf...
  • WordPress REST Api和Ember.js样板 该存储库将帮助您快速设置项目,以通过wp-json / v2 api插件从Wordpress网站获取数据。 项目结构 --app |---adapter |---application.js // handle connection to wordpress |-...
  • <p>I am developing two simple sites using wordpress and other using PHP i want to exchange data between these two mutually using Rest.</p> <p>I must be able to post data into or get data from ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,333
精华内容 533
关键字:

apirestwordpress