How to Log Custom Events with the Activity Log Plugin
Content
Many WordPress administrators and developers use the Activity Log plugin to monitor changes on their sites. A common need is to extend this logging capability to track actions within custom plugins, themes, or specific scripts. This guide explains how you can leverage the plugin's built-in function to add your own custom log entries.
Why Log Custom Events?
The Activity Log plugin excels at tracking standard WordPress events like post updates, user management, and plugin installations. However, many sites run on custom code—unique admin actions, data import scripts, or specialized plugin functions. Tracking these activities is crucial for a complete audit trail, debugging, and understanding user actions beyond WordPress's core features.
The Solution: The aal_insert_log() Function
Based on discussions in the plugin's support forums, the primary method for adding custom logs is a function called aal_insert_log(). This function allows you to insert a record directly into the plugin's custom database table, automatically capturing details like the current user, timestamp, and IP address.
How to Use the Function
You can call aal_insert_log() anywhere in your custom plugin or theme's code, provided the Activity Log plugin is active. The function requires an array of parameters to define the log entry.
if ( function_exists( 'aal_insert_log' ) ) {
$args = array(
'action' => 'updated', // The type of action (e.g., created, updated, deleted)
'object_type' => 'Custom Data', // The type of object being acted upon
'object_subtype' => 'Daily Sync', // A more specific subtype (optional)
'object_name' => 'MySQL Data Correction', // A name or title for the object
'object_id' => 123, // The ID of the object (optional)
);
aal_insert_log( $args );
}
Parameter Breakdown:
- action: A verb describing the event (e.g., 'exported', 'corrected', 'ran').
- object_type: A general category for the event (e.g., 'Plugin', 'Theme Option', 'Stock Movement').
- object_subtype: (Optional) A more specific sub-category.
- object_name: A human-readable description of the specific event.
- object_id: (Optional) The ID of the affected item, if applicable.
The function handles the user, IP address, and date automatically, so you only need to focus on describing the action itself.
Real-World Example: Logging a Data Correction
As mentioned in one support thread, a user had a custom script for correcting data in a MySQL table. They wanted to log each time a manual correction was made. Here is how they could implement it:
// This code runs when a data correction is performed
if ( function_exists( 'aal_insert_log' ) ) {
aal_insert_log( array(
'action' => 'corrected',
'object_type' => 'Data Import',
'object_subtype' => 'Manual Adjustment',
'object_name' => 'Adjusted product stock levels for ID #' . $product_id,
) );
}
Looking at the Plugin's Code for Reference
For developers who want to see how the plugin uses this function internally, the 'Activity Log – Monitor & Record User Changes' team often points to their own code as a reference. For instance, you can see how they log post updates in their GitHub repository within the class-aal-hook-posts.php file. This can provide valuable context for structuring your own log entries.
Important Considerations
- Function Existence Check: Always wrap your call in
if ( function_exists( 'aal_insert_log' ) ). This prevents fatal errors if the Activity Log plugin is ever deactivated. - Data Storage: The plugin stores all log entries in a custom database table, which is efficient and separate from the standard
wp_poststable. This helps maintain site performance even as the log grows. - No Annotations: It is important to note that, as per other support threads, the plugin does not currently have a feature that allows users to manually add annotations or comments explaining why a change was made. The
object_nameparameter is your best tool for adding descriptive context.
By using the aal_insert_log() function, you can seamlessly integrate logging for your custom code, creating a unified and comprehensive activity history for your WordPress site.
Related Support Threads Support
-
Annotationshttps://wordpress.org/support/topic/annotations-3/
-
Custom eventshttps://wordpress.org/support/topic/custom-events-3/
-
Is there a way to add custom log items?https://wordpress.org/support/topic/is-there-a-way-to-add-custom-log-items/
-
Feature Request: Tracking Employee Stock Movements on Our Shopping Sitehttps://wordpress.org/support/topic/feature-request-tracking-employee-stock-movements-on-our-shopping-site/
-
custom module logginghttps://wordpress.org/support/topic/custom-module-logging-1/
-
Record custom activityhttps://wordpress.org/support/topic/record-custom-activity/
-
Custom rules for custom roleshttps://wordpress.org/support/topic/custom-rules-for-custom-roles/
-
Admin Columns integration / custom columnshttps://wordpress.org/support/topic/admin-columns-integration-custom-columns/
-
Data storage – core tables or custom tablehttps://wordpress.org/support/topic/data-storage-core-tables-or-custom-table-3/
-
No plugin edits recordedhttps://wordpress.org/support/topic/no-plugin-edits-recorded/
-
Feature request: comment on actionshttps://wordpress.org/support/topic/feature-request-comment-on-actions/
-
Log custom eventhttps://wordpress.org/support/topic/log-custom-event/
-
Woocommerce changes not logginghttps://wordpress.org/support/topic/woocommerce-changes-not-logging/
-
api loghttps://wordpress.org/support/topic/api-log/
-
? Use as a change log / site documentation aid (allow manual entries)https://wordpress.org/support/topic/use-as-a-change-log-site-documentation-aid-allow-manual-entries/
-
Display Tracking in MyAccount for each user?https://wordpress.org/support/topic/display-tracking-in-myaccount-for-each-user/
-
Export restrictions for Woocommercehttps://wordpress.org/support/topic/export-restrictions-for-woocommerce/
-
Send activity log via webhookhttps://wordpress.org/support/topic/send-activity-log-via-webhook-2/
-
Custom Eventshttps://wordpress.org/support/topic/custom-events-5/
-
Can I track user post activity?https://wordpress.org/support/topic/can-i-track-user-post-activity/