admin_ajax() — Thesis Skin API Method

If you need to add an ajax behavior to your Skin, then you need to add an action (a hook) that fires when your ajax event occurs.

Typically, you’d just include the appropriate event/action declaration in your construct() method, and you could indeed do that here.

In this case, however, Skins already have quite a few ajax events in place, so a cleaner, more organized approach is simply to add your event/action to the existing list of Skin ajax events.

By using the admin_ajax() method, you can add your custom ajax events to a bulletproof place in your Skin:

protected function admin_ajax() {
	add_action('wp_ajax_my_custom_action', array($this, 'my_ajax_method'));

public function my_ajax_method() {
	global $thesis;
	/* The code you place here will run whenever your ajax action occurs. */
	if ($thesis->environment == 'ajax') die();

In the above code, the admin_ajax() method is self-explanatory, but my_ajax_method() requires additional explanation.

In that method, notice global $thesis and also the if statement at the end—these are necessary to ensure that Thesis exits the ajax process properly. You’ll want to include them in your ajax methods just as you see them here.