Wordpress REST Api: add_action( 'rest_api_init', callback) does not call the callback

Viewed 1190

Problem:

I'm trying to register a custom endpoint for a Wordpress plugin. The problem I face is that when I call the add_action('rest_api_init', callback), the callback function is not being called. In that callback function lives the "register_rest_route()" method, which in it's turn is not being called and I am unable to register any custom endpoints.

  • I'm using docker for development
  • No errors are being thrown

Code:

public function register()
    {
        $this->setup_init();

    }
    
    public function setup_init()
    {
        var_dump('print1');

        add_action('rest_api_init', array($this, 'register_custom_endpoints'));
    }
    
    public function register_custom_endpoints()
    {

        var_dump('print2');
        die();

        register_rest_route('test', '/test', array(
            'methods'  => 'GET',
            'callback' => 'menu_setup',
        ));
    }

Question:

The code reaches the "var_dump('print1')", but the the "var_dump('print2')" is never reached. Am I missing something here?

1 Answers

After trying many options I found out that changing: "Setting -> permalinks -> common settings" to anything else then the option "Plain" solved the issue. The callback method is now being reached, and my custom endpoints are being registered.

enter image description here

Related