I am trying to add custom endpoints (custom endpoint” mean an arbitrary URL not recognized by WP as a standard URL, like a permalink or so.) I use WordPress plugin boilerplate "https://github.com/DevinVinson/WordPress-Plugin-Boilerplate".
I want to send an API request on a custom endpoint. I create a new class file to register endpoints and include this class file in the plugin activate method and call add_action hook but it's not working.
Create a new class-rigal-custom-Endpoint.php file in the includes folder.
<?php
class Rigal_custom_Endpoint {
private static $initiated = false;
public static function init() {
if ( ! self::$initiated ) {
self::init_hooks();
}
}
public static function init_hooks() {
self::$initiated = true;
add_action('init', 'add_endpoint');
}
/**
*
* @description Create a independent endpoint
*/
public static function endpoint() {
global $wp;
$endpoint_vars = $wp->query_vars;
// if endpoint
if ($wp->request == 'exercise/test') {
// Your own function to process endpoint
exit;
}
}
}
After I Include this class file in the plugin activate method. (class-rigal-plugin-activator.php)
public static function activate() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-rigal-custom-endpoint.php';
add_action( 'init', array( 'Rigal_custom_Endpoint', 'init' ) );
add_action( 'parse_request', array( 'Rigal_custom_Endpoint', 'endpoint' ) , 0);
}
When I activate the plugin and add "exercise/test" In URL but it's not working. Would you please suggest a solution to fix the above issue?
Environment PHP: 7.4 Wordpress: 5.4 OS: Mac: 10.15.1 Browser: Chrome:Version 81.0.4044.122
Thanks