Comments (6)
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.
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.
Thanks for the help @mtx-z. Just one question: the method must be static or could be public?
from controller.
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.
@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.
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)
- Undefined variable $last when debugging with only one class HOT 1
- How to run code in constructor only if current page matches the controller ? HOT 2
- Remove composer/installers dependency? HOT 1
- ACF vars with dash HOT 3
- Controller not working on custom post type with underscores HOT 3
- Error with woocommerce and ACF Invalid argument supplied for foreach() HOT 2
- Question: How to modify the loop? HOT 4
- Blade debug throws errors or not much data HOT 1
- Suddenly stopped working HOT 7
- Sage 10 HOT 7
- add Comment HTML HOT 1
- Release new version HOT 3
- Partials folder conflicts with theme's partial folder. HOT 1
- Can't use "Category" as a Controller
- Pass ACF options fields from controller HOT 1
- 9.0.0-beta.4 PHP 7.4 class ordering bug resulting in Fatal Error HOT 7
- PHP 8.0.3 warnings HOT 1
- Static function Snake or Camel Case? HOT 2
- Fatal error if ACF expected and not activated
- Getting ACF data on Archive page HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from controller.