This video is quick and dirty talk about the WPAlchemy_MediaAccess helper class which will help you integrate the WordPress Media Uploader with your custom meta boxes.
This video is quick and dirty talk about the WPAlchemy_MediaAccess helper class which will help you integrate the WordPress Media Uploader with your custom meta boxes. It’s hot off the code editor (alpha stage) but functions rather well. Please give me your initial impressions/feedback.
View the setup and install WPAlchemy video if you need a quick primer on getting it all setup properly.
file: functions.php
include_once 'metaboxes/setup.php'; include_once 'metaboxes/custom-spec.php';
file: metaboxes/setup.php
include_once WP_CONTENT_DIR . '/wpalchemy/MetaBox.php';
include_once WP_CONTENT_DIR . '/wpalchemy/MediaAccess.php';
// include css to help style our custom meta boxes
add_action( 'init', 'my_metabox_styles' );
function my_metabox_styles()
{
if ( is_admin() )
{
wp_enqueue_style( 'wpalchemy-metabox', get_stylesheet_directory_uri() . '/metaboxes/meta.css' );
}
}
$wpalchemy_media_access = new WPAlchemy_MediaAccess();
file: metaboxes/custom-spec.php
$custom_mb = new WPAlchemy_MetaBox(array ( 'id' => '_custom_meta', 'title' => 'My Custom Meta', 'template' => get_stylesheet_directory() . '/metaboxes/custom-meta.php', ));
file: metaboxes/custom-meta.php
<?php global $wpalchemy_media_access; ?>
<div class="my_meta_control metabox">
<?php $mb->the_field('imgurl'); ?>
<?php $wpalchemy_media_access->setGroupName('nn')->setInsertButtonLabel('Insert'); ?>
<p>
<?php echo $wpalchemy_media_access->getField(array('name' => $mb->get_the_name(), 'value' => $mb->get_the_value())); ?>
<?php echo $wpalchemy_media_access->getButton(); ?>
</p>
<?php $mb->the_field('imgurl2'); ?>
<?php $wpalchemy_media_access->setGroupName('nn2')->setInsertButtonLabel('Insert This')->setTab('gallery'); ?>
<p>
<?php echo $wpalchemy_media_access->getField(array('name' => $mb->get_the_name(), 'value' => $mb->get_the_value())); ?>
<?php echo $wpalchemy_media_access->getButton(array('label' => 'Add Image From Library')); ?>
</p>
</div>
Using MediaAccess Field/Button with MetaBox Repeating Fields Functionality
file: metaboxes/custom-meta.php (e.g. metaboxes/repeating_fields_meta.php)
<?php global $wpalchemy_media_access; ?>
<div class="my_meta_control">
<p><a href="#" class="dodelete-docs button">Remove All</a></p>
<?php while($mb->have_fields_and_multi('docs')): ?>
<?php $mb->the_group_open(); ?>
<a href="#" class="dodelete button">Remove</a>
<?php $mb->the_field('imgurl'); ?>
<?php $wpalchemy_media_access->setGroupName('img-n'. $mb->get_the_index())->setInsertButtonLabel('Insert'); ?>
<p>
<?php echo $wpalchemy_media_access->getField(array('name' => $mb->get_the_name(), 'value' => $mb->get_the_value())); ?>
<?php echo $wpalchemy_media_access->getButton(); ?>
</p>
<?php $mb->the_field('title'); ?>
<label for="<?php $mb->the_name(); ?>">Title</label>
<p><input type="text" id="<?php $mb->the_name(); ?>" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
<?php $mb->the_group_close(); ?>
<?php endwhile; ?>
<p style="margin-bottom:15px; padding-top:5px;"><a href="#" class="docopy-docs button">Add</a></p>
</div>
You can get the code on github.

Hello there,
Something weird with the media uploader: I use the “wp_get_attachment_image_src” function to display my images, which works correctly for brand new uploaded (from the computer) images, but fails in the case of yet existing images picked in the library: the URL is saved as meta date, but there is no more attachment with the post…
Am I doing something wrong?
Thanks!
Hi there,
I got this error message
Fatal error: Call to a member function setGroupName() on a non-object in mypath\custom-meta.php on line 5
Can you explain this ?
Thanks !
Hello,
I’had the same problem.
in yourtheme/metabox/setup.php
After:
function my_metabox_styles() { if ( is_admin() ) { wp_enqueue_style( 'wpalchemy-metabox', get_stylesheet_directory_uri() . '/metaboxes/meta.css' ); } }add this line:
Hi Dimas,
Amazing work on with WP Alchemy, I am really amazed at how clean and flexible this code is. Adding the Media Access functions is an exponential leap as well. Thanks!
I am having a small issue, which may not be easily fixed. I have a CPT that I would like to attach two galleries to. I was originally going to have one gallery be created using the standard Attachment method, and then have meta fields pointing to the other images. With Media Access I was hoping I could include the second gallery as images uploaded via this meta-box – of course those images are just added to rest of the post gallery.
Probably no way around this eh? Short of excluding those images from the gallery?
Hi
where would I set the upload path for it to be different from the regular media upload button at the top of the post edit screen?
thanks
All wordpress uploads go into the “wp-content/uploads” folder, you can change this globally in your “Settings > Media” options
I know that. What I mean is I want uploads managed by WP Alchemy to go to a different folder.
I know there’s an ‘uploads_dir” filter hook for example. Maybe I can conditionally change the uploads folder when uploading with the wp alchemy metaboxes?
The “MediaAccess” class is a very simple tie-in to the WordPress Media Management System (mms), basically returning the path of the image … you could use a form based browse button and bypass the wp mms all together, or as you’ve mentioned, use the wp hook to bypass (you would have to set a flag somewhere in the process to know where the request is coming from).
How do I go about outputting this in a template. specifically grabbing the imgurl from a certain page and displaying it on such. I have tried numerous things to no avail.
I’m using Netbeans, too…..
Hello,
I currently use the WordPress Media upload but I would like to add a sticker next to the delete button. Is this possible? If so, can you help me.
Thank you in advance
Hi!how to display the above code
in my theme?
Hi Dimas,
Is there a way to prevent images from being added to the gallery?
Thanks for all the hard work!
great library here but i faced a problem with the latest wp and i’m wondering whether this is an issue. Following all the instruction here, everything looks good until i start uploading images the insert button still said ‘Insert into Post’ instead of the one i customized it which cause it to keep inserting into the editor, any idea?
Could you please provide an example of how to save the value of ‘imgurl’ when the post is publsihed and then put it back in the field when the post is edited.
Thanks
Hi Dimas
Is there a way to prevent images from being added to the gallery?
Is it possible to retrieve the attachment id instead of the url and at the same time have some kind of useful feedback (not just the id#) to be shown on the dashboard?
Thanks for a great plugin,
John
Hi Dimas,
Good Day, I copied the same code for the media repeater however i kept getting error Fatal error: Call to a member function setGroupName() on a non-object in C:\wamp\www\site\wp-content\themes\windsorblinds\backend\functions\metaboxes\product-meta.php on line 38
version 1.5.2
Thanks,
Tony
How to echo the fields into my theme? Using
foreach ($meta['docs'] as $link)I get “Invalid argument supplied for foreach()”
Ooops, my falut, I messed up variable names. It’s working now. Echo the fields using:
global $custom_mb;
$meta = $custom_mb->the_meta();
foreach ($meta['docs'] as $link)
{
echo $link['imgurl'];
}