How to integrate Prevent Direct Access Gold with Frontend File Manager Plugin

Frontend File Manager is a simple plugin allowing your users to upload files from frontend by themselves. With this tool, you don’t need to grant them permission to access the admin dashboard to manage their uploads, which might cause risks for your site.

When a file is uploaded via Frontend File Manager Plugin, it will be automatically stored in a new subfolder under the WordPress upload directory. Since the folder is named after the user who uploaded files, it might help you manage files effectively. However, this also causes conflict with other plugins due to this unusual behavior.

To make Prevent Direct Access Gold plugin to work properly with Frontend File Manager, you need to add some extra code into your functions.php file. Remember to edit the file stored under the child theme, so they won’t be removed when you update the theme.

add_action('pda_s3_pre_sync', 'pda_fix_nmedia_files'); // Add this line if you are using PDA S3 Integration
add_action('pda_before_protect_file', 'pda_fix_nmedia_files');

function pda_fix_nmedia_files($attachment_id)
{
    if (!is_callable('wpfm_get_file_path_by_id')) {
        return;
    }
    $parent_id = wp_get_post_parent_id($attachment_id);
    if (empty($parent_id)) {
        return;
    }
    $upload_dir = wp_upload_dir();
    $nmedia_file = wpfm_get_file_path_by_id($parent_id);
    if (!isset($nmedia_file) || !is_file($nmedia_file)) {
        return;
    }
    $nmedia_file_file = str_replace($upload_dir['basedir'] . '/', '', $nmedia_file);
    update_post_meta($attachment_id, '_wp_attached_file', $nmedia_file_file);
}
Lasted updated on May 15, 2020