Over the last decade, the WordPress platform has grown one of the most widely used content management systems in the world. However, users will start to need non-standard endpoints as a result of this growth. These non-standard endpoints are not covered by wp-admin or the default notes of WordPress’s REST API. This means you will need to create these non-standard endpoints from scratch. Here, we’ll take you through some of the most widely used strategies for dealing with these situations, including adding your own custom rewrite rules.
You will need to create a custom endpoint when you need to route a specific request through to a specific code to process it. This is often required when responding to webhooks, processing settings field in the admin, or simply handling requests that result in the creation of a file.
In the past, there were a number of primary options that could be used to create a custom endpoint. Using admin-ajax.php became the standard solution because WordPress site users are encouraged to copy the conventions of core development when creating plug-ins for your own WordPress site. Without the option of creating a custom REST API endpoint, this was really the only available option.
The WordPress REST API encourages and lends itself well to testable code in a way that other parts of the WordPress platform don’t. In the right context, using a custom endpoint of the WordPress REST API can provide great results. However, it’s important that you understand in some situations this solution won’t work.
Using HTTP Foundation Component
When you’re working with situations that don’t make sense to use custom REST API endpoints, you should turn to the HTTP Foundation Component. This will allow you to use modern PHP tools for your WordPress site. An HTTP Foundation Component will provide an object-oriented abstraction that developers can also use for testing purposes.
To start using the HTTP Foundation Component, you will need to install and choose a version using composer. Symfony development will automatically keep up to date with any PHP development. This means you should be using PHP7 on the WordPress platform. Make sure that you’re also supporting older versions of PHP as well.
The HTTP Foundation will enable you to setup Request and Response objects. They will give you the object-orientated way of dealing with any HTTP requests. This is great as this tends to be missing in many of the traditional solutions.
How Far to Take Custom Endpoints for Your WordPress Site?
The main point of this article was to introduce the HTTP Foundation Component and encourage WordPress users to explore this component when creating custom endpoints. Once you are familiar with this, you will be able to easily work with the majority of PHP frameworks. How much you use the HTTP component in your applications, plugins and WordPress site will be dictated by your needs. Learning how to add custom endpoints to your WordPress site can be invaluable.
Hopefully, this article introduces you to the concept of the HTTP component. Let us know in the comments below if you make use of the HTTP component.