wp_lib_dash_api_ - roberts-sandbox/create-repo GitHub Wiki
###Description
This action hook allows developers to modify a specific call to the Dash API, a request to retrieve data or modify the library without reloading the Dashboard page. This could be used to respond to a custom API request or to modify an existing API request.
Call this hook with the name of the API request you want to modify/create. So, to modify WP-Librarian's behaviour when scanning an item's barcode, you'd use the hook wp_lib_dash_api_scan-barcode.
###Context
To access the Library Dashboard users must be at the minimum a librarian and thus logged in as a WordPress user. No nonce validation has yet happened as nonces are specific to the Dash page they came from.
To use you this hook you
###Parameters
$ajax_api
The instance of WP-Librarian's Dashboard API class. Access to this is necessary to handle completing the AJAX request properly.
###Example
Suppose you want an API request that allows the Dashboard to get a Member's name given their ID. The following code would allow this:
add_action('wp_lib_dash_api_member-name', 'get_member_name');
function get_member_name($ajax_api) {
// Helps prevent user from forging HTTP requests
$ajax_api->checkNonce('My nonced page');
// If the member ID is valid, return member name
if (isset($_POST['member_id']) && ctype_digit($_POST['member_id']) && get_post_type($_POST['member_id']) === 'wp_lib_members') {
$ajax_api->addContent(get_the_title($_POST['member_id']));
} else {
$ajax_api->addContent(false);
}
// WordPress AJAX requests must always kill script execution when they're finished
die();
}
###See Also
[[wp_lib_dash_api]]
[[wp_lib_dash_action]]
[[wp_lib_dash_action_]]
[[wp_lib_dash_page]]
[[wp_lib_dash_page_]]