Four strategies for adding custom endpoints to WordPress sites

Over the past 14 years, WordPress has grown into a fully flexible content management system. However, as these grow, you need to add non-standard endpoints to the workflow. These endpoints are not included in the default annotations of the topic template hierarchy, WP admin, or WordPress rest API, so they must be created from scratch. This article will introduce general strategies to deal with this situation, such as the idea of customizing rewrite rules and adding custom rest API endpoints to provide the various methods that exist. Then, we will use the \
Viewing an existing solution adding a custom endpoint is what happens when you must route to specific code to handle very specific requests. This may be to handle requests for an administrator to set fields, respond to a web hook, display a user interface, or generate a file (such as CSV, ). In the past, you had several basic options that you wanted to discuss one at a time. In the initial hook (such as init), check the super global $\u get or $\u post for specific conditions, and run the code after passing. Use the custom rewrite rule admin Ajax. It’s easy to add init or early hook using PHP to connect to a custom rest API endpoint. It’s not a good solution, but it works. This is not good because all implementations are temporary and depend on everyone who follows the rules set by each implementation. The
Admin ajax Using PHP is standard because it is recommended to copy the core development rules from the built-in plug-ins on our website. If there is no appropriate API, using it is an option, not an option. Because I use different reactions. Although it is not a very good system, the efficiency is also very low. These solutions work closely with the WordPress plug-in API and PHP superglobal, making it difficult to write unit tests. A system that uses one of these two solutions to run correctly and fully functions is often too strongly connected to the current HTTP request. In other cases, when the same logic must be run, the replica is not available and must be used forcibly. Cosmetic solutions. The
Adding custom rewrite rules is probably the most \
The WordPress rest API is designed in this way. Handle the request and return the callback class of wp\u rest\u response object
\/**@inheritdoc*\/
Public function getresponse(): response
The{
$response= new response ($this->getimage();
$response= new binaryfileresponse ($ths->getimage();
$response->setcontentdisposition (\symfony\component\httpfoundation\responseheaderbag:: disposition\u attachment);
Return $response.
}The
}How far will you take it? The point of this article is not to regenerate word press routes using the latest OOP PHP. The focus is on introducing HTTP foundation component, an excellent tool, and encouraging users to browse the document. In addition, once you are familiar with this component, you can use most PHP frameworks. The last point is very important. I like WordPress, but it is not the best tool for all tasks. As a PHP and JavaScript developer, there are many excellent tools that can be used with or instead of WordPress. I want to use all these tools. Depends on application, plug-in, and site usage. But I like you to learn and use it.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *