WordPress自定义API端点开发:文件配置及教程咨询
Building Custom WordPress REST API Endpoints: A Step-by-Step Guide
Got it, let's walk through this clearly—you’ve got the docs down, now let’s turn that knowledge into action. Here’s exactly where to put your code and how to get started:
Where to Place Your Code
You’ve got two solid options, each with pros and cons:
- Child Theme’s
functions.php: The quickest way for small, theme-specific endpoints. Just paste your code at the bottom of this file. Pro tip: Always use a child theme instead of your parent theme—this way, theme updates won’t wipe out your custom code. - Custom Plugin: The recommended approach for most use cases. Plugins are independent of your theme, so they’ll keep working even if you switch themes. Here’s how to set one up:
- Create a new folder in
wp-content/plugins/(e.g.,my-custom-api-endpoints). - Inside that folder, create a PHP file (e.g.,
my-custom-api-endpoints.php). - Add the required plugin header at the top of the file:
<?php /** * Plugin Name: My Custom API Endpoints * Description: Adds custom REST API endpoints to WordPress * Version: 1.0 * Author: Your Name */ - Now you can add your endpoint registration code right below this header.
- Create a new folder in
Step-by-Step Example: Creating a Basic Endpoint
Let’s build a simple endpoint that returns the 5 most recent posts. Add this code to either your child theme’s functions.php or your custom plugin file:
// Register our custom endpoint add_action('rest_api_init', function () { // Define the namespace and route register_rest_route( 'my-custom-api/v1', // Namespace (helps avoid conflicts) '/recent-posts', // Route URL (will be /wp-json/my-custom-api/v1/recent-posts) array( 'methods' => 'GET', // Accept GET requests 'callback' => 'get_recent_custom_posts', // Function to run when the endpoint is hit 'permission_callback' => '__return_true' // Allow public access (adjust for private endpoints!) ) ); }); // Callback function that returns the data function get_recent_custom_posts() { $args = array( 'post_type' => 'post', 'posts_per_page' => 5, 'orderby' => 'date', 'order' => 'DESC' ); $recent_posts = get_posts($args); // Format the posts into a readable array $response = array(); foreach ($recent_posts as $post) { $response[] = array( 'id' => $post->ID, 'title' => get_the_title($post->ID), 'url' => get_permalink($post->ID), 'date' => get_the_date('Y-m-d', $post->ID) ); } return rest_ensure_response($response); // Return a proper REST response }
Key Notes:
- Namespace: Always use a unique namespace (like
my-custom-api/v1) to prevent conflicts with other plugins or WordPress core endpoints. - Permission Callback: Replace
__return_truewith a custom function if you need to restrict access (e.g., only allow logged-in users). For example:'permission_callback' => function () { return current_user_can('edit_posts'); } - Response: Use
rest_ensure_response()to make sure your output follows WordPress REST API standards (proper headers, JSON formatting).
Learning by Doing: Next Steps
To deepen your understanding, try these small projects:
- Create an endpoint that accepts POST requests to update a custom field.
- Add query parameters to your
recent-postsendpoint (e.g.,?category=newsto filter by category). - Implement error handling—use
new WP_Error()to return meaningful error messages when something goes wrong.
You don’t need external tutorials beyond the official docs once you start experimenting. Start small, test your endpoints with tools like Postman or curl, and tweak as you go.
内容的提问来源于stack exchange,提问作者Ahmed ElShemy
相关产品推荐
相关产品推荐

