Aug 07, 2018

Media Library Folders Pro Actions & Filters: 3 Examples To Get You Started

Media Library Folders Pro Actions & Filters: 3 Examples To Get You Started

Media Library Folders Pro is designed to be intuitive for both non-developers and developers alike. Through the beginner-friendly interface, you can create folders, move files around, rename files, and plenty more.

But we also recognize that you might want to go further and customize Media Library Folders Pro’s functionality to your own site’s unique needs. To make that easier, we’ve built a detailed system of actions and filters that developers can use to modify or extend Media Library Folders Pro’s functionality.

In this article, we’re going to share three specific examples of the type of helpful functionality you can use these actions and filters to accomplish.

Click here if you want to view a full list of every single action and filter available.

Automatically Save Certain Filenames Or File Types To A Specific Folder

As part of your folder structure, you might have a specific folder to which you upload all files with a certain naming structure or extension.

For example, if you upload PDF eBooks, you might have a dedicated eBooks folder to house all those PDFs. Without actions and filters, you’d need to manually browse to that folder whenever you want to upload a new PDF. Now, that’s certainly not a huge tink, but you can actually speed up your workflow by using the mlfp_filter_upload_destination filter.

Based on an uploaded file’s filename or file type, you can use this filter to force all newly uploaded files into the specified folder automatically.

To get started, you’ll need the ID of the folder you want to force files to. To find this ID, hover over the folder in the Media Library Folders Pro interface and look at the URL:

Media Library Folders Pro actions and filters

In the example above, the folder ID is 737.

Now, you can add the mlfp_filter_upload_destination filter to your theme’s functions.php file (or a plugin like Code Snippets):

add_filter('mlfp_filter_upload_destination', 'filter_upload_destination', 10, 2 );
function filter_upload_destination($new_filename, $folder_id) {
  $extension = pathinfo($new_filename, PATHINFO_EXTENSION);
  if($extension == 'pdf')
    $folder_id = 737;
  return $folder_id;
}

 

Make sure to replace $extension with the actual extension you want to target and $folder_id with the actual folder ID you want to automatically upload files to.

Create A Log Of Moved Or Renamed Files At Your Site

If you’re regularly moving and/or renaming files, or if you allow other users to move/rename files at your site, you might want to create a log so that you have a record of everything that’s happened.

With the mlfp_after_file_move action or the mlfp_after_file_rename action, you could create a new wp_media_library_log table in your database and then automatically add the new and old location or name.

For example, here’s how you could use the mlfp_after_file_move action to log file movements after you’ve created the table in your database:

add_action('mlfp_after_file_move','do_after_file_move');
function do_after_file_move($image_path, $destination_name, $destination_url) {
  global $wpdb;
  $table = $wpdb->prefix . "media_library_log";
  $data = array(
    'oldpath' => image_path,
    'newpath' => destination_name
  );
  $wpdb->update($table, $data);
}

 

Send An Email When A New File Is Uploaded

Next, let’s say you want to receive an email whenever a new file is uploaded. This might be overwhelming for a large site with dozens of uploads daily, but it’s a great way to stay on top of less busy sites.

Using Media Library Folders Pro’s mlfp_after_add_file action and the core wp_mail function, you can automatically send an email detailing each newly uploaded file.

add_action('mlfp_after_add_file', 'process_after_add_file', 10, 2);
function process_after_add_file($attach_id, $filename, $folder_id) {
  $admin_email = "admin@example.com";
  $title = "New file added";
  $message = "The file, $filename, was just uploaded.";
  $headers = 'From: ' . $admin_email . "\r\n" .
    'Reply-To: ' . $admin_email . "\r\n" .
    'MIME-Version: 1.0' . "\r\n" .
    'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
  wp_mail($user_email, $title, $message, $headers);
}

 

Other Ways To Extend Media Library Folders Pro With Hooks

We’ve shown you three examples above, but that’s not even close to the full list of actions and filters available to Media Library Folders Pro users.

The plugin includes actions for:

  • Before/after adding a file
  • Before/after creating a new folder
  • Before/after moving or copying a file
  • Before/after renaming a file
  • Before/after deleting or hiding a file or folder
  • Before/after adding a category
  • Before/after syncing a file or regenerating thumbnails
  • Before/after adding a file to a MaxGalleria or NextGEN Gallery

And the plugin also has filters for:

  • Toolbar buttons and areas
  • File caption text
  • File folder destination

To learn more about how you can use all of these actions and filters, head to the full documentation.