WordPressdidn’t become the world’s most popular content management system by being exclusive. The tool is free, open-source, beginner-friendly, highly flexible, and always evolving.

According to many experts, the next step in the evolution of WordPress is its REST API, and we think that makes it worth checking out.

The WordPress REST API vastly expands WordPress’s capabilities — it opens the CMS up to more easily connect with other programs. Now, besides being a way to build and manage websites, developers can leverage WordPress like a service, taking only the functionality they need. It’s a new way to build with WordPress.

APIs are a more complex topic in the realm of marketing. So, in this guide, we’ll explain what APIs are and what REST is. Then, we’ll explore how developers use the WordPress REST API to bypass its limitations. Let’s get started.

Grow Your Business With HubSpot's Tools for WordPress Websites

If you’re new to the terms API and REST, that definition might not make complete sense just yet. Let’s briefly cover what APIs and REST APIs are and why they matter here.

What is an API?

An application programming interface, or API, is a part of a piece of software that allows two applications to communicate with each other. An API exposes a portion of an application’s data and functionality, enabling integration by third-party applications.

For example, say you wanted to display a YouTube video on your website. Since the video is owned by YouTube and stored on a YouTube server, you have to request the video through YouTube’s API. After the API receives and approves your request, it sends the video back to you, and you can embed the video as desired.

Or, say you want your application to upload a video to YouTube — YouTube’s API lets you do this as well. This time, you make a different request asking to upload your content. If the API approves, the video uploads without requiring you to log into a YouTube user account.

APIs make today’s software ecosystem possible. Through APIs, disparate software applications can integrate and share data in an automated and standardized way. Whether it’s your automation software scheduling your social media posts or the weather app on your phone, most of your favorite apps employ APIs in some way.

要了解有关API的工作以及何时使用的更多信息,请参阅我们的API完整指南

What is a REST API?

当我们在网络上谈论API时,我们通常在谈论一种称为REST API的API。休息,代表性状态转移的缩写是一组构建Web API的准则。遵循这些准则的API是REST API(或Restful API)。

有关REST API的更详细信息,请查看我们的专用帖子。目前,我们可以简要总结REST API的五个要求,因为它们控制了WordPress REST API的工作原理。这些要求是:

  • Client-server separation:托管API的应用程序(称为“服务器”)和使用API​​的应用程序(称为“客户端”)作为单独的系统保存。他们只能通过客户端的请求进行通信,然后进行服务器的响应。
  • 统一界面:All requests and responses must follow a common protocol. In most cases, REST APIs use the Hypertext Transfer Protocol (HTTP) to communicate over the internet.
  • Stateless:All interactions with an API are independent of each other. Every request and response should contain all information necessary to complete the interaction without referencing previous requests/responses.
  • Layered system:很少有客户端和server directly connected over a network. Usually, proxy servers exist between them to handle traffic, performance, and security. RESTful requests and responses should always be formatted the same, regardless of how many layers exist between client and server.
  • Cacheable:如果客户端从服务器请求资源(即数据),则服bob体育苹果系统下载安装务器应指示提供的资源是否可能为cached by the clientand for how long.

These constraints standardize how web applications talk to each other. They ensure that any interaction with any REST API follows a similar process. It also keeps the two applications independent, such that changes or upgrades to either application will not affect communication between them.

WordPress REST API做什么?

The WordPress REST API is, as the name implies, a REST API, so it operates along the guidelines listed above.

Specifically, the WordPress REST API exposes WordPress's inner workings to outside developers, which makes it possible to integrate WordPress with other applications in numerous ways. And, since REST is so common in web APIs, this makes the WordPress REST API relatively easy for any developer to learn and use.

Originally developed as an open-sourceWordPress plugin, the WordPress REST API was added to WordPress core in version 4.7, released in 2016. This was a major step for WordPress, effectively turning it into a software platform. Now, developers could build with WordPress without having to buildwithinit.

For example, you might want to build a web application from scratch but harness WordPress’s content management and article drafting features. With the WordPress REST API, you can fetch post content from your WordPress account and then publish it on your custom web application. Or, you might connect WordPress to a mobile app you’re building with the REST API. You’re probably familiar with theGutenberg block editor- 这是Web应用程序的真实示例,该应用使用WordPress REST API在WordPress数据库中检索和修改内容。

WordPress REST API还为开发人员解锁了强大的灵活性。没有它,开发人员将需要使用WordPress(WordPress的服务器端脚本语言)对WordPress进行修改。现在,开发人员可以在WordPress的约束之外编程,并以不同的语言(例如前端编程语言JavaScript)进行编程。

Lastly, note that the WordPress REST API is one ofWordPress维护的几个API。These other WordPress APIs mainly exist to allow plugins and themes to interact with WordPress core.

How to Use the WordPress REST API

Before we experiment with posting and receiving WordPress data, let’s review some key terms to know for using the WordPress REST API.

WordPress REST API Terms

首先,一个requestis what you send to the WordPress REST API. It specifies what resource in the WordPress database you want to target, and what you want to do with it.

Aresponse是API在您的请求后发回的。它包含您要求的任何数据,以及确认请求是否成功或错误消息(如果没有)。

Aroute是用于通过WordPress API找到资源的URL,例如帖子,页面,元数据,用户或其他数据类型。例如,使用“ http://yourwebsite.com/wp-json/wp/v2/pages”路由来访问WordPress网站上的页面。

Routes are used inendpointsto access or modify data through the API. An endpoint combines a route with a command called an HTTP method. The four HTTP methods you’ll probably use most often are:

  • GET:This method retrieves data from the server.
  • 邮政:此方法将数据添加到服务器。
  • 放:This method edits data already on the server.
  • 删除:This method removes data from the server.

例如,从yourwebsite.com检索所有WordPress页面的端点看起来像这样:


GET http://yourwebsite.com/wp-json/wp/v2/pages

端点为yourwebsite.com添加新页面的端点看起来像这样:


发布http://yourwebsite.com/wp-json/wp/v2/pages

TheWordPress Developer Referencelists all of the REST API’s routes and endpoints, which let you access and manipulate virtually anything on your WordPress site. Most if not all of your interactions with the WordPress API will utilize these endpoints.

Depending on how you use the REST API, you may also need to provideauthenticationto make requests. For many requests, WordPress needs to verify who you are first. By default, most public content on your website you can access with the API without authentication, while accessing private data requires authentication.

Make a Request to the WordPress REST API

有了基本的术语,我们现在已经足够知道可以向REST API发送简单的请求。我们不建议您在您的实时网站上测试REST API - 而是在服务器或本地设置测试网站以提出请求。

The simplest way to make manual API requests is through the command-line interface (CLI), a program with which you write text-based commands to your computer’s operating system. On Windows, the default CLI program is Command Prompt. On macOS, it’s Terminal.

To communicate with an API from the command line, you can usecURL, a free tool to make HTTP requests (among many other types of requests). You might also tryWP-CLI, a free command-line interface specifically for WordPress that lets you interact with your WordPress website without using your browser.

To start, use cURL or WP-CLI to access your remote or local test site via SSH. If prompted, enter your credentials for authentication.

Once you’ve accessed your test site, you can start sending requests. The base route for your API requests is


http://yourwebsite.com/wp-json/

…“ yourwebsite.com”是您的领域。您将在此URL的末尾添加子目录,以请求特定的数据。

让我们从上一个示例开始,从WordPress网站获取所有页面。此操作的终点是:


GET http://yourwebsite.com/wp-json/wp/v2/pages

If instead you want to retrieve a specific page, you can do that by adding the pageIDto the end of the route:


GET http://yourwebsite.com/wp-json/wp/v2/pages/

… whereis the numeric id of the page.

Next, let’s experiment with the POST method. We can use POST to add a new page to a website with the following syntax:


发布http://yourwebsite.com/wp-json/wp-json/wp/v2/pages

This adds a blank page to your website which you can populate with content.

What if you want to get information about a specific user in your WordPress account? This is also possible with a simple request:


GET http://yourwebsite.com/wp-json/wp/v2/users/

… whereis the numeric id of the user.

We’re only scratching the surface of what’s possible here – to make the most of the WordPress REST API, your requests will be much more complex. However, if you’re just getting the hang of WordPress APIs and APIs in general, it’s worth practicing simpler requests to gain a solid grasp of the fundamentals.

WordPress REST API Examples

我们已经看到了开发人员如何通过WordPress的REST API索取信息并进行更改 - 现在,让我们查看一些有关此API的样子的示例。

Gutenberg

The WordPress Gutenberg editor application implements the WordPress REST API to access page and post information. It fetches page and post data, including text and media, from the WordPress database and allows you to place your content with blocks — a simple example of the API at work.

product page for gutenberg, a wordpress rest API example

Image Source

AppPresser

AppPresser lets users create iOS and Android mobile apps that integrate with WordPress websites. It calls the WordPress REST API to retrieve content from WordPress and to modify database contents as dictated by your mobile app.

AppPresser的产品页面,WordPress REST API示例

Image Source

Wired

WordPress的发布后不久,美联社I as a part of WordPress core, popular technology publication Wired used the API to make a synced copy of their main site for testing. Whenever the main website updated its content, the test site changed to match.

product page for wired.com, a wordpress rest API example

Image Source

事件浓缩咖啡

事件浓缩咖啡is an event management and ticketing plugin for WordPress that makes use of the WordPress REST API. With an add-on, site owners can make event information available to developers, so event information can be used by external apps and websites.

事件咖啡的产品页面,WordPress REST API示例

Image Source

When to Use the WordPress Rest API

The WordPress REST API is great for two main cases. First, if you want to integrate WordPress into an application or website you’re building (that isn’t itself made with WordPress), you can accomplish this with the REST API.

其次,如果您更喜欢使用PHP以外的其他语言开发,例如客户端和/或服务器端JavaScript,则可以使用您选择的语言创建主题和插件,并利用API访问所需的数据。

However, there are cases where this API isn’t necessary. Namely, if you want to develop a theme or plugin with PHP, try leveraging WordPress’s other APIs, like thePlugin APIand主题自定义API。Website owners can also accomplish a lot looks-wise with the right WordPress theme and some extensive customization.

简而言之,WordPress REST API并不总是正确的解决方案,但是当它是强大的时候。WordPress API文档很好地说:“如果您想要一种结构化的,可扩展的和简单的方法来将数据输入WordPress,则可能要使用REST API。”

Build with WordPress, outside of WordPress.

如果您想了解WordPress CMS的增长和适应方式,那么您将至少了解其REST API的基础知识。WordPress REST API充当通往更大应用生态系统的门户。现在,各种bob电竞官方下载软件可以在工具之外利用WordPress技术。

If you’re not a developer, it still helps to know how the REST API works, just in case you decide to integrate WordPress with your tools in any way. With some extra knowledge, you can diagnose problems, collaborate with devs, and maybe one day make custom integrations yourself.

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

最初发布于2021年10月26日7:00:00 AM,更新于2021年10月26日

Topics:

WordPress Website