Lesson 6 – How to use MainWP Actions & Filters

For developers and advanced users, MainWP provides hooks in the form of actions and filters, allowing extensive customization and integration capabilities. This lesson will delve into how to effectively use MainWP actions and filters to tailor your MainWP experience to your needs.

Understanding Actions & Filters

Before diving into MainWP-specific hooks, it’s important to understand the fundamental concepts of actions and filters in WordPress:

  1. Actions are functions executed at specific points during WordPress execution or when specific events occur. They allow you to add or modify functionality.
  2. Filters are functions that WordPress passes data through before taking some action with that data. They allow you to modify how data is handled and displayed.

MainWP extends these concepts by providing its own set of hooks for its unique processes.

 

Using MainWP Actions

MainWP actions allow you to hook into specific events in the MainWP lifecycle. Here’s how to use them:

  1. Identify the Action Hook: Refer to the MainWP Developer Documentation to find the appropriate action hook for your needs.
  2. Create a Custom Function: Write a function that performs the task you want to execute when the action occurs.
  3. Hook the Function: Use add_action to attach your function to the identified hook.

Here’s an example of using an action hook to perform a task when a new site is added to the MainWP Dashboard:

<?php
function my_custom_task_on_new_site( $site ) {
  // Custom code to run when a new site is added
  error_log( 'New site added: ' . print_r( $site, true ) );
}
add_action( 'mainwp_site_added', 'my_custom_task_on_new_site' );
?>

Using MainWP Filters

MainWP filters allow you to modify data at specific points. The process is similar to using actions:

  1. Identify the Filter Hook: Find the appropriate filter hook from the MainWP documentation.
  2. Create a Custom Function: Write a function that modifies the data as required.
  3. Hook the Function: Use add_filter to attach your function to the identified hook.

Here’s an example of using a filter hook to modify the data before it is displayed in the MainWP Dashboard:

<?php
function my_custom_modify_dashboard_data( $data ) {
    // Modify the data as needed
    $data['my_custom_field'] = 'Custom Value';
    return $data;
}
add_filter( 'mainwp_dashboard_data', 'my_custom_modify_dashboard_data' );
?>

Practical Examples

To further illustrate the use of MainWP actions and filters, here are a couple of practical examples:

Example 1: Custom Email Notification on Site Update

You may want to send a custom email notification every time a site connected to MainWP is updated.

<?php
function my_custom_email_on_site_update($site) {
    $to = 'admin@example.com';
    $subject = 'Site Updated';
    $message = 'The site ' . $site['name'] . ' has been updated.';
    wp_mail($to, $subject, $message);
}
add_action( 'mainwp_site_synced', 'my_custom_email_on_site_update' );
?>

Example 2: Modify Backup Directory

You might want to change the directory where backups are stored:

<?php
function my_custom_backup_directory( $directory ) {
    return '/custom/backup/directory/';
}
add_filter( 'mainwp_backup_directory', 'my_custom_backup_directory' );
?>

Debugging and Testing

When working with actions and filters, it’s crucial to test thoroughly:

  • Use logging (error_log) to debug and track data.
  • Test your code in a staging environment before deploying to production.
  • Ensure compatibility with MainWP updates and other plugins.

Conclusion

MainWP actions and filters provide powerful tools for customizing and extending the functionality of your MainWP Dashboard. By understanding and utilizing these hooks, you can tailor your MainWP setup to better meet your specific needs, streamline workflows, and enhance your WordPress management capabilities.