ajax(sample)

Description

Display a single or set of buttons that trigger an ajax call when clicked.

The Ajax Button option is useful when you want to allow users to perform or process something in the backend from within your options area.

This option does an ajax call to admin-ajax.php, so you will need to create a wp_ajax_{action} action hook. See the codex for details on how to use Ajax in the Administration.

You can add a single button or multiple buttons that call different ajax calls. Specify the arguments as a single string to create a single button, or place an array of strings for multiple buttons.

This option does not save anything and is used for performing tasks when a user initiates with a click.

Notes on Creating Ajax Handlers

When creating ajax handlers via the wp_ajax_{action} action hook, instead of returning the function normally, you will need to call either a wp_send_json_success() or a wp_send_json_error() so that the option can detect the result of the ajax call.

Option Details & Parameters

Type: ajax-button

Version: 1.8

Can be placed in: Admin Panels , Admin Tabs , Meta Boxes , Theme Customizer

Parameter Type Description
name string The name of the option, for display purposes only.
id string A unique ID for this option. This ID will be used to get the value for this option.
desc string The description to display together with this option.
action string/array The name or an array of names of the ajax actions to call when the button is clicked. Upon a click, the wp_ajax_{action} is called.
label string/array The label of the button/s. Defaults to Click me
class string/array (Optional) The class to use to adjust the look of the button/s. Valid values are button-secondary or button-primary. Defaults to button-secondary.
wait_label string/array (Optional) The label for the button/s when it is in its waiting state (after its clicked and waiting for the call to finish). Defaults to Please wait....
success_label string/array (Optional) The label for the button/s after a successful ajax call. Defaults to blank.
error_label string/array (Optional) The label for the button/s after an ajax call that resulted in an error. Defaults to blank.
success_callback string/array (Optional) The name of a Javascript function (within the scope of window) to call after a successful ajax call. Use admin_enqueue_scripts to add your scripts. Defaults to blank/none.
error_callback string/array (Optional) The name of a Javascript function (within the scope of window) to call after an ajax call that resulted in an error. Use admin_enqueue_scripts to add your scripts. Defaults to blank/none.
data_filter_callback string/array (Optional) The name of a Javascript function (within the scope of window) to call additional post data in the ajax request .Use admin_enqueue_scripts to add your scripts. Defaults to blank/none.

Examples

Creating an Ajax Button that does something:

$panel->createOption( array(
'name' => 'Perform Action',
'type' => 'ajax-button',
'action' => 'my_action',
'label' => __( 'Perform It!', 'default' ),
) );

Then somewhere in your project…

add_action( 'wp_ajax_my_action', 'perform_my_action' );
function perform_my_action() {
// Do something
$result = do_something_somewhere();
if ( $result ) {
wp_send_json_success( __( 'Done!', 'default' ) );
}
wp_send_json_error( __( 'Failed!', 'default' ) );
}

Creating multiple Ajax Buttons, the first one is the primary button:

$panel->createOption( array(
'name' => 'Perform Action',
'type' => 'ajax-button',
'action' => array( 'my_action1', 'my_action2' ),
'label' => array(
__( 'Perform it!', 'default' ),
__( 'Or this!', 'default' ),
),
'class' => array( 'button-primary', 'button-secondary' ),
) );

Then somewhere in your project…

add_action( 'wp_ajax_my_action1', 'perform_my_action1' );
function perform_my_action1() {
// Do something
$result = do_something_somewhere();
if ( $result ) {
wp_send_json_success( __( 'Done!', 'default' ) );
}
wp_send_json_error( __( 'Failed!', 'default' ) );
}
add_action( 'wp_ajax_my_action2', 'perform_my_action2' );
function perform_my_action2() {
// Do something
$result = do_something_somewhere();
if ( $result ) {
wp_send_json_success( __( 'Done!', 'default' ) );
}
wp_send_json_error( __( 'Failed!', 'default' ) );
}