WP Activity Log includes many features designed for different use cases, allowing users to get the most out of the plugin in different scenarios. Those looking to extend this functionality to fit niche use cases or integrate custom processes can also make use of the plugin’s hooks. These hooks allow WP Activity Log to extend its functionality beyond what’s included in the box.
Hooks can be used to keep track of virtually any change on your WordPress website. One notable example is WooCommerce users, who may need to track additional events and details related to their store.
WP Activity Log can track WooCommerce activities, including store settings changes, product updates, orders, and many others straight out of the box. By default, the plugin reports important data in the activity log, such as the product name, ID, and SKU, the username and role of the user who made the change, and other relevant information. Administrators, however, may need to include additional information, such as product stock quantities, for example. This can be achieved through hooks.
For a complete list of changes the plugin tracks, refer to the list of event IDs used in the WP Activity Log.
Stay one step ahead: Get your free copy of ‘The Ultimate Guide to WordPress Oversight’ today!
In this how-to, we will show you how to add product stock quantities to the activity log by creating a mu-plugin.
Example: Adding the product’s stock quantity in the activity log with a mu-plugin.
What is a mu-plugin?
MU stands for must-use. A must-use plugin is a type of plugin that WordPress will always load, hence why it’s called a must-use. Creating a mu-plugin is easy. All you need to do is save the below code in a PHP file, then upload it to the /wp-content/mu-plugins/ directory on your WordPress website file system.
Before you upload the mu-plugin
Kindly make sure you have WP Activity Log installed and activated before uploading the mu-plugin since this is a requirement for this feature to work. You might also want to test the plugin in a WordPress testing environment before uploading it to your production environment to ensure that all steps have been followed correctly.
mu-plugin code example
The below code is provided on an as-is basis as an example. You can make changes to the code depending on your requirements. Having some PHP experience is recommended.
?php
/**
* Adds product stock quantity metadata for events 9000-9025.
*
* @param array $metadata Event data.
* @param integer $code Event ID.
*
* @since latest
*/
function wpws_emdt_change_metadata_definition( $metadata, $code ) {
if ( $code > 9000 && $code < 9025 ) {
$metadata[ __( Stock quantity, 'wpws_emdt' ) ] = '%stockQuantity%';
}
return $metadata;
}
add_filter( 'wsal_event_metadata_definition', 'wpws_emdt_change_metadata_definition', 10, 2 );
/**
* Populates custom metadata item product SKU with the desired value.
*
* @param array $event_data Event data.
* @param integer $event_id Event ID.
*
* @since latest
*/
function wpws_emdt_add_items_to_metadata( $event_data, $event_id ) {
if ( $event_id > 9000 && $event_id < 9025 && array_key_exists( 'PostID', $event_data ) && function_exists( 'wc_get_product' ) ) {
$product = wc_get_product( $event_data['PostID'] );
if ( $product instanceof WC_Product ) {
$event_data['stockQuantity'] = $product->get_stock_quantity();
}
}
return $event_data;
}
add_filter( 'wsal_event_data_before_log', 'wpws_emdt_add_items_to_metadata', 10, 2 );
More about hooks and WP Activity Log
Hooks offer a flexible way to extend the functionality of WP Activity Log, allowing you to add new custom events and modify existing ones. This flexibility ensures that WP Activity Log can cater to the most unique and demanding scenarios, helping you stay on top of your WordPress administration game.
Working with and using WordPress activity log hooks is easy, with no need for advanced development skills to get the job done.
Monitor user and system activity with support for 3rd party plugins and session management.
Get WP Activity Log and start logging your WooCommerce activities today.