Support for two key features make this WordPress meta box PHP class even more attractive. With the addition of filtering and repeating fields, you have even more flexibility when using meta boxes for your themes and plugins.
My latest update to the WPAlchemy_MetaBox
class (v1.1) is a pretty good one. There are two notable features in this release:
Filtering
The filtering capabilities will allow you to filter your meta boxes by template, categories, tags and post/page IDs.
I often find myself writing specific meta boxes for certain sections of a website, with this functionality, I can limit the display of the meta boxes so as not to clutter up other pages which don’t need them.
The following example will exclude all pages that use the “product” template except for a specific product page:
new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta',
'title' => 'My Custom Meta',
'template' => TEMPLATEPATH . '/custom/meta.php',
'exclude_template' => array('product.php'),
'include_post_id' => 85
));
The following filters your meta box by excluding it from several specific posts, unless those posts have the “download” tag:
new WPAlchemy_MetaBox(array
(
'id' => '_custom_meta',
'title' => 'My Custom Meta',
'template' => TEMPLATEPATH . '/custom/meta.php',
'exclude_post_id' => array(45,48,56,57,88,91,92,93)
'include_tag' => 'download'
));
Repeating Fields
With this added functionality you can create repeating fields and field groups.
A simple example would be a meta box for adding a list of links: you may have two fields, one for a title and the other for a URL (field group), using this feature of the class you can easily setup an “add new” link/button that would automatically spawn new instances of the field group you defined.
Setting up a repeating field region is pretty straight forward, read the documentation to learn more and see an example.
Download
WPAlchemy MetaBox Class, this project is on github
Extended Documentation
The documentation page has been overhauled, the class is well documented. As I make updates, I will be documenting the changes. As you begin to use the code, please let me know any issues you may have.
This is so awesome. The features you’ve included in here are really something. I’ll be working with this for a while and will for sure report back with feedback. Thanks a lot Dimas! Also, any word on that coffee/beer button?
Hi.thanks for your great class, impressive work!
I’ve just test it on WordPress 3 for custom post type and it’s very easy!
I specially like the unlimited field option.
I have test it on a custom box for infinite slider on home page (with an upload btn to choose an image in media library) and it’s very cool!
I wondering if your class is tested for a long time because i’m interested to use it for my clients website?
@Derbois, I’ve just recently created the class (mid May), but I’ve been working with meta boxes since April (and working with PHP for years).
The class is a culmination of what I’ve learned so far about wordpress meta boxes, my usage of meta boxes in client work, the themes I’ve worked on, and the plugins I’ve developed.
At Hitch Creative I use it for our client work.
I definitely want to make sure the class is robust enough for developers to use, I am always available to answer questions about it, so I hope that you do give it a try for your client work.
Thanks for your quick answer. I think Iwill test it for my next project.
One question I’m trying to use the “media box uploder with tickbox” to write values of “group fields (for an unlimited image slider).
I use a jquery script to open the media uploader and to return the image url to the generated field.
the problem is that i need to record the post with a value in generated field before being able to have my upload script working.
Have you ever trying to do such thing?
Perhaps you can help me?
Here’s my js script:
Here’s my (your) metabox script:
[edited] added <pre> … </pre> tags around code, < and > still need to be used inside <pre>
@Derbois I think I kinda understand what you are trying to do (or maybe not) … I think i generally understand the direction you are trying to go with, using the “add media box” to upload an image, then get that image URL into your meta box, no?
I assume you are trying to target the generated field with jQuery? If so, there are a couple of things you can try:
The class has a
get_the_index()
andthe_index()
function, you can use this to create unique IDs for certain HTML elements, you can then use this in javascript to properly target returned values.Additionally you can try using jQuery’s
parents([selector])
and other traversing functions to accurately target the input that you need.Yes you have understand.
Sorry for my bad english ;).
I think i have a Jquery problem , I will try to fix it.
Thanks for your help
I have succeed to do my unlimited uploader (url return to the field from the wordpress media browser).
My code is a bit “tricky” but if I’ll enhance.it, I will share it.
The problem was in the “clone js script” in your MetaBox class. Your original clone script duplicate form elements “id” thats why my other script(which return the url to the field couldn’t work) (the duplicated field with the “display:none” field and with the class ” last tocopy” haved the same Id as the field with the class “last “.
@Derbois, I am glad you found your solution … let me know what adjustments you made to solve your problem, this will definitely help me improve the code overall.
Hi Dimas, I am using WPAlchemy and I love it, but I really don’t know how to do this (because I am not a developer) :
I am trying to create a metabox with a list of books on a custom post type named “Authors”, so the user can check the books the author wrote.
I take the list of books from the db, store them in
$the_books
array and try to create a checkbox and a label for every book using WPAlchemy loop :while($metabox->have_fields('book', sizeof($the_books))):
Perhaps my thinking here is wrong, but how would you create something like this ?
When I check a book and update page, the value is saved fine and I can read it fine on the frontend, but I can’t make it display right in the admin. The problem I think is that when I create this inputs and labels, I use
$metabox->the_name()
but can’t use$metabox->the_value()
because I have to display the name of the books from the array :$the_books[$count_books]->name;
.Hope you understood the problem and let me know if you think this is possible.
Thank you.
Here is the full code I am using in the metabox template :
Horia, I think your pretty close and seems like you already know what the solution will be, try something like:
nice library!
there are know bugs/issues?
ps
do you have a google group?
@daniele, the code has definitely evolved and has become more stable, the code is available on github and I make adjustments as needed. I use if in my own production code.
There currently isn’t a discussion group for the code, but I am looking into something better than the WordPress commenting system for discussion. For now you can post your comments and questions on the wpalchemy page.
Excellent work here; I’ll just be throwing out the meta_box functions in all the themes I modify and using yours from here on out.
Could you help me troubleshoot something? I’m pretty much following your example exactly for an endlessly repeating field, but the “Add” button isn’t actually replicating the field.
Here is how I create the meta_box instance:
$sidebar_images_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_sidebar_images_meta',
'title' => 'Sidebar Images',
'template' => TEMPLATEPATH . '/custom/sidebar_images_meta.php',
'types' => array('page'),
'exclude_template' => 'index.php',
'priority' => 'high'
));
Here is my template:
Again, just can’t get the field to replicate when clicking “Add Another Image.” Any ideas?
Hello! Considering repeating fields:
I’ve been using WPAlchemy on sites for a while now without problems. Now I’ve stumbled upon a problem I can’t seem to solve properly.
Is it possible to nest two or more ‘have_fields_and_multi’ loops? Every time I insert one into another the script breaks and creates an infinite loop with incorrect field nesting (it keeps creating the same elements inside each instance of itself). In pseudo-code my implementation looks like this:
while ( $mb -> have_fields_and_multi( ‘foo’ ) ) {
// Open a new container for a parent item listing
// e.g. a category of sorts. These should be able
// to be added as the user wishes.
while ( $mb -> have_fields_and_multi( ‘bar’ ) ) {
// Make repeating fields inside the parent
// category loop.
} // child loop
} // parent loop
(I’ve properly inserted group opens and closes, also the add/delete buttons in their correct locations.)
This creates an infinite loop and the controls are not working properly (e.g. the inner loop has a ‘dodelete’ -button, but it removes everything including the parent loop in its entirety).
Is there any workaround to get this functioning properly or should I try some other approach?
@Otto, at the moment there isn’t a way to do nested repeating fields. I’ve started a working on a solution and will try to have something available soon.
@Dimas: Okay, thanks for the info! I’ll see if I can manage some other solution to this in the meantime. 🙂