php - Syntax issue on rest object's callback

I had issues settiing a route registration function until I remembered to add the namespace to it:add_action('rest_

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
Add a comment  | 

1 Answer 1

Reset to default 1

The 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' ],
] );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745309063a4621891.html

相关推荐

  • php - Syntax issue on rest object's callback

    I had issues settiing a route registration function until I remembered to add the namespace to it:add_action('rest_

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信