I'm using the WP REST API to trigger a task from an AJAX request, my callback function attempts to trigger an action found within a plugin, however, that plugin doesn't appear to have loaded.

The following code is within my functions.php file:

add_action('rest_api_init', function() {

    register_rest_route('foo/v1', '/capture-payments', array(
        'methods' => 'POST',
        'callback' => [$this, 'capturePayments'],


public static function capturePayments($request) {

    // this action doesn't load, because the plugin hasn't loaded
    if ( ! has_action('name_of_plugin_action') ) {
        return new \WP_Error('payment_capture_not_available', 'Payment capture is not available', array('status' => 500));



My question is, is there a way to load the plugins for this API request, such that I can access their functionality as I would within a normal theme?

Edit 1:

It might be worth noting that this used within the admin, for purely backend purposes.

  • Where are you adding this action?
    – kero
    Commented Oct 11, 2018 at 11:43
  • has_action is checking if a particular hook as been used for add_action(), but that doesn't appear to be what you're using it for. Do you mean function_exists(). Also, this code has public static function, which implies it's in a class. Is this code actually in a class? Commented Oct 11, 2018 at 11:48
  • My code is all within functions.php. The plugin in question exposes an action that I want to trigger using do_action('name_of_plugin_action'), which is what I'm checking using has_action(), I want to make sure the action exists before I continue, and it seems I'm using that correctly. Commented Oct 11, 2018 at 11:58

1 Answer 1


I have figured out the problem, after trying a load of things I wanted to double check plugins weren't being loaded at all within the REST API.

I added some rudimentary debugging to the plugin in question and I saw that the plugin was indeed being loaded, my issue was that in these conditions the plugin itself wasn't exposing functionality it would do under a normal request.

The do_action I wanted to achieve therefore was failing, as it hadn't been registered. I simply invoked the class that registered the action and now my code works as desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.