4
votes

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

1

1 Answers

-1
votes

Because the activation method is only executed during the plugin activation. Once the plugin is active, this method will no longer be executed.

In class-plugin-name.php on load_dependencies method, add your class, for example:

require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-rigal-custom-endpoint.php';

in define_public_hooks add your action

$this->loader->add_action( 'parse_request',  'Rigal_custom_Endpoint', 'endpoint' );

This will work, but take in consideration that you don't register your endpoint api in the standard way. You should take a look at the WP REST API documentation:
https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

This is an example of minimal plugin using register_rest_route


<?php
/*
Plugin Name: Add WP Custom endpoint
Author: Robin Ferrari
Version: 1.0
*/

class MYPLUGIN_Api_Endpoint{
    public function init(){
        add_action( 'rest_api_init', array($this, 'register_rest_routes'));
    }
    public function register_rest_routes(){
        register_rest_route( 'myplugin/v1', '/exercise/test', array(
            'methods' => 'GET',
            'callback' => array($this, 'exercise_test'),
        ));
    }
    /**
     * Your custom route callback
     */
    public function exercise_test(){
        $error = false;
        // do what you whant
        if($error){
            return new WP_Error(500, "KO");
        }
        return new WP_Rest_Response(array(
            'status' => 200,
            'message' => "OK",
        ));
    }
}
$api = new MYPLUGIN_Api_Endpoint();
$api->init();

And then your endpoint is accessible here:

https://example.com/wp-json/myplugin/v1/exercise/test