Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin tommcfarlin/00-class-ajax-manager.php Secret
Last active Jul 29, 2016

Embed
What would you like to do?
[WordPress]
<?php
namespace Acme\Admin;
class Ajax_Manager {
public function register( $object, $method ) {
add_action(
"wp_ajax_$method",
array( $object, $method )
);
}
}
<?php
public function __construct( $ajax_manager ) {
// If options exist, then use them; otherwise, initialize an empty array.
$this->options =
get_option( 'acme-options' ) ?
get_option( 'acme-options' ) :
array();
/* For this example, assume there is a default set of options called
* `$default_options` that are used whenever no options are present
* for the given options key above.
*
* Merge the arrays. The default options must go first so the existing
* options override the default options.
*/
$this->options = array_merge( $default_options, $this->options );
/**
* Assume that the options are composed of key/value pairs and we're
* only using the event options.
*/
$event_partial = new Views\Partials\Event( $this->options['event'] );
// Tell the Ajax Messenger the event partial has an Ajax callback.
$ajax_manager->register( $event_partial, 'the_taxonomies' );
}
<?php
$submenu = new Display\Submenu(
new Display\Submenu_Page( new Admin\Ajax_Manager() )
);
$submenu->init();
var send_ajax_request = function( cpt_id ) {
// Define the function and the data to send to the server.
var data = {
'action': 'the_taxonomies',
'post_type': cpt_id
};
// Send the request to the server and handle the response.
$.post( ajaxurl, data, function( response ) {
// Handle your response here.
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.