Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

WP_ajax #56

Closed
mtx-z opened this issue Nov 7, 2017 · 6 comments
Closed

WP_ajax #56

mtx-z opened this issue Nov 7, 2017 · 6 comments

Comments

@mtx-z
Copy link

mtx-z commented Nov 7, 2017

Hello !

I would like to know if there's a way to use SoberWP controllers with WP_ajax ?
I tried to put the add_action into my controller __construct, but as it's an Ajax call, the constructor is never called and so the action never triggered...

Any way we could handle it ?
Thanks !

@mtx-z
Copy link
Author

mtx-z commented Nov 7, 2017

Found one way to do it:

In function.php (or related file executed for each request)

add_action( 'wp_ajax_nopriv_action_name', [ \App\MyController::class, 'methodController' ] );
add_action( 'wp_ajax_action_name', [ \App\MyController::class, 'methodController' ] );

Works to use a Controller method for a wp_ajax action. add_action cannot be placed into Controller constructor as it's not instanciated on ajax call.

@marcelo2605
Copy link

Thanks for the help @mtx-z. Just one question: the method must be static or could be public?

@mtx-z
Copy link
Author

mtx-z commented Jun 11, 2018

I haven't putted it in a method. It's directly in the function.php file. I guess you could put it in a method hooked on the "init" filter maybe.
So no static/public here as we're not in a Sober Controller.

EDIT: Ah you're talking about the called method from the Controller. Well I don't know if I have tested it. If you do so, please report here !

@marcelo2605
Copy link

@mtx-z I believe it is only possible use static functions because public functions is running when the page is load (I'm using Sage starter theme).

@mtx-z
Copy link
Author

mtx-z commented Jul 4, 2018

Tested, and it works with public methods.

using

add_action( 'wp_ajax_nopriv_action_name', [ \App\MyController::class, 'methodController' ] );
add_action( 'wp_ajax_action_name', [ \App\MyController::class, 'methodController' ] );

I'm able to ajax call

namespace App;
use Sober\Controller\Controller;

class MyController extends Controller {

    public function methodController() {
        ...
    }
}

The action callback will itself instantiate the controller, and call the matching method from this instance (I guess).

@abelthomas
Copy link

abelthomas commented Mar 7, 2019

You can define the wp_ajax_ actions within the __construct() method of a controller. The trick is to define the callback method as a static function, instead of public.

Here's an example:

// `app/controllers/FrontPage.php`

class FrontPage extends Controller {
  /**
   * Constructor
   */
  public function __construct() {
    // handle ajax requests
    add_action( 'wp_ajax_do_something', [$this, 'do_something'] );
    add_action( 'wp_ajax_nopriv_do_something', [$this, 'do_something'] );
  }

  /**
   * AJAX request callback
   *
   */
  public static function do_something() {
    // code code beep boop
    $response = "I'm heading off to the land of javascript.";
    echo $response;
    die();
  }
}

If the callback were defined as a normal public function, the die() statement would break the page, since all public functions are automatically exposed to the respective view. Defining the function as static circumvents this, while still allowing the function to run.

NOTE: you can't call a private or protected method from within a publicly instantiated class, so that's why we must use the static visibility.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants