April 15, 2013 at 7:43 am #2561
I use WPAlchemy in a responsive theme framework I’m developing. I was in the process of cleaning it up for release when I realized that MediaAccess.php used eval(), which is frowned upon. I created an alternative to the WPAlchemy_MediaAccess class that takes advantage of the new media manager. I’m quite pleased with the results, and thought I’d share with anyone else who is interested.
The solution includes a few php functions, and a short jQuery script. The script could also be printed in the footer, but I prefer to enqueue scripts conditionally.
First, the PHP.
Place these functions somewhere before you define your metaboxes.
If your metabox will use a media button, enqueue the script using ’init_action’ => ‘media_upload’ when defining the metabox.
To instantiate a media button in your metabox, use the function wpa_media_button() before or after the corresponding text input. Both the button and input must be wrapped in a container element such as a <p> or <div>.
The function takes 4 parameters:
-text you want in the button in your metabox
-title of the media manager window that will pop up
-text you want in the media manger ‘insert media’ button
-type of files you want to allow in the media manager for this field – possible types are image, video, audio, or application (pdfs). Leave blank for all media types.
Somewhere in your meta.css, you should style the button targeting the .upload-button class. (oops, I meant to change that to .media-button.)
Now for the JS.
Short and sweet. I had a heck of a time coming up with this script, as the documentation for the new media manager is sparse. I found quite a few examples on the web for integrating an insert media button into your plugin etc, but all were much more complicated and flawed under certain circumstances. I am new to backbone.js, so there might be some flaws to my approach, but I was unable to break it or produce any errors in my testing.
Let me know if anyone has any ideas on how to improve this approach, or if you find a bug.
- This topic was modified 10 months, 4 weeks ago by Aargh-a-Knot. Reason: forgot to include info about media types
You must be logged in to reply to this topic.