I had issues settiing a route registration function until I remembered to add the namespace to it:
add_action('rest_api_init', '\DelayedCoupons\registerDummyRoute');
But now I believe I have the same issue in a class and I can't seem to solve it. What error am I making below?
// main file
namespace DelayedCoupons;
require_once ('adminSettingsArea/ApiController.php');
use \admin\controllers\ApiController;
$apiController = new ApiController();
add_action('rest_api_init', [$apiController, 'registerDummyResponse']);
// class file required above
namespace admin\controllers;
class ApiController extends \WP_Rest_Controller {
protected $namespace = 'delayedCoupons';
protected $version = '1.0';
protected $urlBase;
public function __construct() {
$this->urlBase = $this->namespace . '/' . $this->version;
}
protected function respondWithString() {
wp_send_json('dummy response');
}
public function registerDummyResponse() {
register_rest_route($this->urlBase, 'dummy', [
'methods' => 'GET',
// 'callback' => '\admin\controllers\ApiController\respondWithString'
// 'callback' => 'ApiController\respondWithString'
'callback' => 'respondWithString'
]);
}
I had issues settiing a route registration function until I remembered to add the namespace to it:
add_action('rest_api_init', '\DelayedCoupons\registerDummyRoute');
But now I believe I have the same issue in a class and I can't seem to solve it. What error am I making below?
// main file
namespace DelayedCoupons;
require_once ('adminSettingsArea/ApiController.php');
use \admin\controllers\ApiController;
$apiController = new ApiController();
add_action('rest_api_init', [$apiController, 'registerDummyResponse']);
// class file required above
namespace admin\controllers;
class ApiController extends \WP_Rest_Controller {
protected $namespace = 'delayedCoupons';
protected $version = '1.0';
protected $urlBase;
public function __construct() {
$this->urlBase = $this->namespace . '/' . $this->version;
}
protected function respondWithString() {
wp_send_json('dummy response');
}
public function registerDummyResponse() {
register_rest_route($this->urlBase, 'dummy', [
'methods' => 'GET',
// 'callback' => '\admin\controllers\ApiController\respondWithString'
// 'callback' => 'ApiController\respondWithString'
'callback' => 'respondWithString'
]);
}
Share
Improve this question
edited Jul 20, 2019 at 16:18
Sean D
asked Jul 19, 2019 at 23:14
Sean DSean D
3878 silver badges21 bronze badges
1 Answer
Reset to default 1The callback has to be public:
public function respondWithString()
And in your register_rest_route()
call, set the callback
to [ $this, 'respondWithString' ]
:
register_rest_route( $this->urlBase, 'dummy', [
'methods' => 'GET',
'callback' => [ $this, 'respondWithString' ],
] );