A lightweight WordPress plugin that tracks key admin actions (login, post updates, plugin activation). Stores logs in a custom database table and provides a simple admin page to view the latest 100 actions.
- Single File Architecture: All code in one PHP file for simplicity
- WPCS Compliant: Follows WordPress Coding Standards
- Secure: Proper sanitization, validation, and escaping
- Singleton Pattern: Prevents multiple instances
- Custom Database Table: Dedicated table for activity logs
- Activity Tracking: Monitors login, post updates, and plugin activation
- Admin Interface: Simple page to view the latest 100 activities
- Translation Ready: i18n/l10n support
- Activation/Deactivation: Proper lifecycle hooks
- Uninstall Cleanup: Clean removal of plugin data and custom table
- Upload the plugin file to
/wp-content/plugins/yt-admin-activity-log/ - Activate the plugin through the 'Plugins' menu in WordPress
- Access the activity log via the admin menu
Once activated, the plugin automatically tracks:
- User Logins: Records when users log into the admin area
- Post Updates: Tracks when posts are created, updated, or deleted
- Plugin Activation: Logs when plugins are activated or deactivated
Navigate to Admin Activity Log in the WordPress admin menu to view the latest 100 activities.
The plugin creates a custom table: {prefix}_admin_activity_log
Columns:
id- Auto-incrementing primary keyuser_id- WordPress user ID who performed the actionaction- Type of action performedobject_type- Type of object affected (post, plugin, etc.)object_id- ID of the affected objectdescription- Human-readable description of the actionip_address- IP address of the usercreated_at- Timestamp of the action
| Action Type | Description |
|---|---|
| Login | User successfully logged in |
| Post Created | New post published |
| Post Updated | Existing post modified |
| Post Deleted | Post moved to trash |
| Plugin Activated | Plugin enabled |
| Plugin Deactivated | Plugin disabled |
YT_ADMIN_ACTIVITY_LOG_VERSION // Plugin version number
YT_ADMIN_ACTIVITY_LOG_BASENAME // Plugin base name
YT_ADMIN_ACTIVITY_LOG_PATH // Plugin directory path
YT_ADMIN_ACTIVITY_LOG_URL // Plugin directory URLget_instance()- Singleton instance retrieval__construct()- Initialize plugininit_hooks()- Register WordPress hooksload_textdomain()- Load translations
create_table()- Create custom activity log tablelog_activity()- Insert activity recordget_recent_activities()- Retrieve latest 100 activities
add_admin_menu()- Add activity log page to admin menurender_activity_log_page()- Display activity log interfaceenqueue_admin_scripts()- Load admin CSS/JS
track_login()- Log user login eventstrack_post_update()- Log post modificationstrack_plugin_activation()- Log plugin state changes
activate()- Run on plugin activation (creates table)deactivate()- Run on plugin deactivationyt_admin_activity_log_uninstall()- Run on plugin deletion (drops table)
// Get plugin instance
$log = YT_Admin_Activity_Log::get_instance();
// Log custom activity
$log->log_activity(
get_current_user_id(),
'custom_action',
'custom_object',
123,
'Custom description'
);
// Get recent activities
$activities = $log->get_recent_activities();Add custom tracking by hooking into the log_activity() method:
add_action( 'your_custom_action', function() {
$log = YT_Admin_Activity_Log::get_instance();
$log->log_activity(
get_current_user_id(),
'your_action',
'your_object_type',
$object_id,
'Your description'
);
} );By default, the admin page shows the latest 100 activities. To modify this, edit the SQL query in get_recent_activities() method.
Implemented:
- Direct file access prevention
- Capability checks (
current_user_can()) - Input sanitization (
sanitize_text_field()) - Output escaping (
esc_html(),esc_attr(),esc_url()) - Prepared SQL statements (
$wpdb->prepare()) - IP address sanitization
- Nonce verification for admin actions
class-yt-admin-activity-log.php # Main plugin file
assets/
css/
admin.css # Admin styles
js/
admin.js # Admin scripts
README.md # This file
- Plugin activates without errors
- Database table is created successfully
- Activity log page displays correctly
- Login events are tracked
- Post update events are tracked
- Plugin activation events are tracked
- Admin interface displays recent activities
- Plugin deactivates cleanly
- Plugin uninstalls and removes all data
- No PHP warnings or notices
- Compatible with latest WordPress version
- Works with PHP 7.4+
Run PHP_CodeSniffer with WordPress standards:
phpcs --standard=WordPress class-yt-admin-activity-log.php- WordPress 5.8 or higher
- PHP 7.4 or higher
- MySQL 5.6 or higher
GPL v2 or later
Built following WordPress Plugin Handbook and WPCS guidelines.
For WordPress plugin development best practices, visit:
- Initial release
- Activity tracking for login, post updates, and plugin activation
- Custom database table for log storage
- Admin interface for viewing activities
- Translation ready