GithubHelp home page GithubHelp logo

WP_ajax about controller HOT 6 CLOSED

soberwp avatar soberwp commented on May 17, 2024
WP_ajax

from controller.

Comments (6)

abelthomas avatar abelthomas commented on May 17, 2024 5

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.

from controller.

mtx-z avatar mtx-z commented on May 17, 2024 4

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.

from controller.

marcelo2605 avatar marcelo2605 commented on May 17, 2024

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

from controller.

mtx-z avatar mtx-z commented on May 17, 2024

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 !

from controller.

marcelo2605 avatar marcelo2605 commented on May 17, 2024

@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).

from controller.

mtx-z avatar mtx-z commented on May 17, 2024

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).

from controller.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.