Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active July 29, 2016 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/549f465734915c13b875d6db7d340e19 to your computer and use it in GitHub Desktop.
Save tommcfarlin/549f465734915c13b875d6db7d340e19 to your computer and use it in GitHub Desktop.
[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