[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' ); | |
} |
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