Rest Route Not Found in Custom REST API Plugin
-
Hello, I’ve been working on creating a custom REST API plugin by myself, and it has been going well. If i test this endpoints one-by-one (by pasting it into the functions.php), the route works. But in my folder, its not working… I keep getting a rest route not found error.
Here is my Folder structure
- custom-plugin
- /includes
- class-custom-plugin.php
- /endpoints
- user-info.php
- bank-info.php
- /products
- test.php
- index.php
- /includes
In my index.php file
<?php /* Plugin Name: Custom API Plugin Plugin URI: https://example.com Description: Enhance the functionality of your WordPress site with the Plugin, a powerful and customizable REST API solution. Version: 2.5 Author: Mike Author URI: https://wa.me/2349xxxxxx License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html */ // Include the main plugin class require_once plugin_dir_path(__FILE__) . 'includes/class-custom-plugin.php'; // Instantiate the plugin class $pxm_api_plugin = new PxmAPI(); **In my class-custom-plugin.php file** <?php class PxmAPI { private $namespace = 'v1'; public function __construct() { add_action('rest_api_init', [$this, 'register_endpoints']); } public function register_endpoints() { // Include endpoint files require_once plugin_dir_path(__FILE__) . 'endpoints/user-info.php'; require_once plugin_dir_path(__FILE__) . 'endpoints/bank-info.php'; require_once plugin_dir_path(__FILE__) . 'products/test.php'; } }
In the includes/endpoints/user-info.php file
<?php // Check if accessed directly if (!defined('ABSPATH')) { exit; } class UserInfoEndpoints { private $namespace = 'v1'; public function __construct() { add_action('rest_api_init', [$this, 'register_endpoints']); } public function register_endpoints() { // User account details route register_rest_route($this->namespace, '/user', [ 'methods' => 'GET', 'callback' => [$this, 'get_userinfo'], 'permission_callback' => [$this, 'check_permissions'], ]); } public function get_userinfo(WP_REST_Request $request) { if (is_user_logged_in()) { $current_user = wp_get_current_user(); $user_id = $current_user->ID; $firstname = $current_user->first_name; $lastname = $current_user->last_name; $user_email = $current_user->user_email; $role = $current_user->roles[0]; $data = [ 'basic' => [ 'firstname' => $firstname, 'lastname' => $lastname, 'email' => $user_email, 'package' => $role, ], ]; return $data; } else { return new WP_Error('unauthorized', 'Invalid Credentials', ['status' => 401]); } } // General User permission public function check_permissions() { return current_user_can('read'); } } // Instantiate the class when the file is included $user_info_endpoints = new UserInfoEndpoints();
In my class-custom-plugin.php file
<?php class PxmAPI { private $namespace = 'v1'; public function __construct() { add_action('rest_api_init', [$this, 'register_endpoints']); } public function register_endpoints() { // Include endpoint files require_once plugin_dir_path(__FILE__) . 'endpoints/user-info.php'; } }
In the bank-info.php
<?php // Check if accessed directly if (!defined('ABSPATH')) { exit; } class BankInfoEndpoints { private $namespace = 'v1'; public function __construct() { add_action('rest_api_init', [$this, 'register_endpoints']); } public function register_endpoints() { // Get user information based on the meta key 'kuda_account' register_rest_route($this->namespace, '/mybank/', [ 'methods' => 'GET', 'callback' => [$this, 'get_user_bybank'], 'args' => [ 'id' => [ 'validate_callback' => function ($param, $request, $key) { return is_numeric($param); }, ], ], ]); } public function get_user_bybank($data) { $bank_account = absint($data['id']); // Check if user exists $user = get_user_by_meta_data('bank_account', $bank_account); if (!$user) { wp_send_json_error(array('message' => 'User not found'), 404); } $user_data = array( 'firstname' => $user->first_name, 'lastname' => $user->last_name, 'email' => $user->user_email, ); wp_send_json_success($user_data); } // Custom function to get a user by meta data function get_user_by_meta_data($meta_key, $meta_value) { // Query for users based on the meta data $user_query = new WP_User_Query( array( 'meta_key' => $meta_key, 'meta_value' => $meta_value, ) ); // Get the results from the query, returning the first user $users = $user_query->get_results(); return $users ? $users[0] : null; } } // Instantiate the class when the file is included $bank_info_endpoints = new BankInfoEndpoints();
In the test.php
<?php // Check if accessed directly if (!defined('ABSPATH')) { exit; } class TestEndpoints { private $namespace = 'v1'; public function __construct() { add_action('rest_api_init', [$this, 'register_endpoints']); } public function register_endpoints() { // Register endpoint for retrieving test posts register_rest_route($this->namespace, '/test-product', [ 'methods' => 'GET', 'callback' => [$this, 'get_test_posts'], 'permission_callback' => [$this, 'check_permissions'], ]); // Add error response for unsupported HTTP methods for test product retrieval register_rest_route($this->namespace, '/test-product', [ 'methods' => 'POST,PUT,PATCH,DELETE,HEAD,OPTIONS', 'callback' => [$this, 'unsupported_method'], ]); } public function get_test_posts() { // Query 'test' posts $args = [ 'post_type' => 'test', // Replace with your custom post type slug 'posts_per_page' => -1, // Retrieve all posts ]; $test_posts = new WP_Query($args); $response = []; if ($test_posts->have_posts()) { while ($test_posts->have_posts()) { $test_posts->the_post(); // Get the post ID $post_id = get_the_ID(); // Get custom fields associated with the post $network = get_field('network'); $discount = get_field('discount'); $type = get_field('type'); // Add the data to the response $response[] = [ 'id' => $post_id, 'network' => $network, 'discount' => $discount . '%', 'type' => $type, ]; } } // Return the response return $response; } // General User permission public function check_permissions() { return current_user_can('read'); } // UNSUPPORTED Request Method public function unsupported_method() { return new WP_Error('unsupported_method', 'Unsupported HTTP Method', ['status' => 405]); } } // Instantiate the class when the file is included $test_endpoints = new TestEndpoints();
I have the file here https://we.tl/t-pnmkrax7Ev
I would appreciate any help please - custom-plugin
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Rest Route Not Found in Custom REST API Plugin’ is closed to new replies.