SOLVED: Media browser (2.4): upload-into-folder only works for HappyFiles, not for custom media folder providers

Bricks version: 2.4

I registered a custom media folder provider through the new API (bricks/media_folder_providers/register, class implementing \Bricks\Media_Folder_Provider). It maps an existing media taxonomy to Bricks folders.

Almost everything works: the folder tree, counts, filtering, “Uncategorized”, create/rename/move/delete, and Move.

The problem: uploading a file while a folder is open always puts it in “Uncategorized”. The folder is never sent to the server.

Cause: the builder JS (assets/js/main.min.js) is hard-coded to HappyFiles:

  • mediaUploadHappyFilesFolderId returns 0 unless mediaFolderProvider.id === 'happyfiles', so no folder ID is sent for any other provider.
  • The upload and “import from URL” requests always send bricksMediaFolderProvider: "happyfiles", not the active provider’s ID.

The PHP side is already provider-neutral: Media_Browser_Upload::assign_requested_folder() compares bricksMediaFolderProvider against the active provider’s get_id(). It never matches, because the JS always sends happyfiles.

Suggested fix: in the JS, use the active provider (mediaFolderProvider.id) instead of the happyfiles literal, both when reading the open folder and for the bricksMediaFolderProvider request parameter.

Steps to reproduce:

  1. Register a custom provider with any ID other than happyfiles.
  2. Open the media browser, open a folder, and upload a file.
  3. The file lands in “Uncategorized”, and the provider’s assign_attachment() is never called.

Still hard-coded in 2.4.3: in main.min.js, mediaUploadHappyFilesFolderId returns 0 for any provider other than happyfiles, and the upload only sends a folder when that value isn’t 0, with the provider written as “happyfiles”.

My own plugin, Refined Media Library, hit the same wall, and the workaround it has shipped since 16 October is plain PHP (two short functions in src/Modules/MediaLibrary/Integrations/BricksBridge.php on its wordpress.org SVN, if you want to read them).

Your provider’s apply_query() runs each time the user opens a folder, inside the bricks_get_pages ajax call, so store that folder ID per user in a short transient there.

Then hook add_attachment: if the request has bricks-is-builder=1 and no bricksMediaFolderId (when there is one, Bricks has already placed the file), assign the new attachment to the stored folder.

Uploads land in the open folder until Bricks sends the active provider’s ID.

We’ve addressed this in Bricks 2.4.4, now available as a one-click update in your WordPress Dashboard.

Please read the changelog entry before updating, and let us know if you experience any issues.