965

WPAlchemy MetaBox PHP Class

The WPAlchemy_MetaBox PHP class can be used to create WordPress meta boxes quickly. It will give you the flexibility you need as a developer, allowing you to quickly build custom meta boxes for your themes and plugins.

  1. Key Features and Benefits
  2. Defining a Meta Box is Easy
  3. Install and Setup (Video)
  4. Meta Box Setup Options
  5. The Guts of the Meta Box
  6. How to Use It! (show class methods)
  7. Repeating Field Groups
  8. Filter Specific Templates, Categories, Tags and Posts
  9. Using The Meta Box Values In Your Template
  10. Download
  11. Contribute

Key Features and Benefits

  • Easy to learn and integrate: good documentation and support is always important (I use the code myself and keep it up-to-date). Integration is a snap, as simple as including the class and using it.
  • Easy setup code: some of the details involved in saving, retrieving and working with the meta data are abstracted to ease development.
  • Flexible usage: the class acts as an aid for meta box development. By design you can use the class functions or your current development practices, which ever you feel most comfortable with for your development.
  • HTML and CSS separation: the HTML and CSS for your meta boxes remain separate from the core code, you can design your meta boxes to your liking, providing you the greatest flexibility during development.

Defining a Meta Box is Easy

// include the class in your theme or plugin
include_once 'WPAlchemy/MetaBox.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' );
	}
}

$custom_metabox = new WPAlchemy_MetaBox(array
(
	'id' => '_custom_meta',
	'title' => 'My Custom Meta',
	'template' => STYLESHEETPATH . '/custom/meta.php'
));

That’s it! The above code shows the basic definition needed to setup a custom meta box.

Tip Use WP_PLUGIN_DIR or WP_PLUGIN_URL if you are embedding wpalchemy in a custom wordpress plugin.

Install and Setup (Easy)

Tip Select 720 HD mode and watch it fullscreen or click the YouTube button to watch it on YouTube.

Setup Options

The following are the different setup options that you can use in the initial class setup array:

id

'id' => '_custom_meta'

Each meta box that you create requires an ID, this is the value that is saved into the wp_postmeta table. Starting your name with an underscore will effectively hide it from also appearing in the custom fields area.

title

'title' => 'Custom Meta'

This is the title of the meta box that appears in the WordPress UI.

template

'template' => STYLESHEETPATH . '/custom/meta.php'

You must define a template file for the contents of your meta box.

types

'types' => array('post','page')

This will default to post and page types, to add your meta box to custom post types you must define the types option.

context

'context' => 'normal'

The part of the page where the edit screen section should be shown (‘normal’, ‘advanced’, or ‘side’).

priority

'priority' => 'high'

The priority within the context where the meta box should show (‘high’ or ‘low’).

autosave

'autosave' => TRUE

Used to decide if meta box content should be autosaved.

mode

'mode' => WPALCHEMY_MODE_ARRAY // defaults to WPALCHEMY_MODE_ARRAY

Can be set to WPALCHEMY_MODE_ARRAY or WPALCHEMY_MODE_EXTRACT. Setting this option to WPALCHEMY_MODE_ARRAY (set by default) will cause the class to store all of its values as an associative array in a single meta entry. Setting this option to WPALCHEMY_MODE_EXTRACT will cause the class to extract all the first tier variables (fields) and create individual meta entries in the wp_postmeta table. This article has more info on this option.

prefix

'prefix' => '_my_' // defaults to NULL

When the mode option is set to WPALCHEMY_MODE_EXTRACT, you have to take care to avoid name collisions with other meta entries. The easiest way to do this is to add a prefix to your variables. This article has more info on this option.

Exclude / Include Options

exclude_… and include_…

exclude_template
exclude_category_id
exclude_category
exclude_tag_id
exclude_tag
exclude_post_id
include_template
include_category_id
include_category
include_tag_id
include_tag
include_post_id

Read more about Filtering Specific Templates, Categories, Tags and Posts

Action / Filter Options

init_action

'init_action' => 'my_init_action_func' // defaults to NULL

Callback used on the WordPress “admin_init” action, the main benefit is that this callback is executed only when the meta box is present.

output_filter

'output_filter' => 'my_output_filter_func' // defaults to NULL

Callback used to override when the meta box gets displayed, must return TRUE or FALSE to determine if the meta box should or should not be displayed.

save_filter

'save_filter' => 'my_save_filter_func' // defaults to NULL

Callback used to override or insert meta data before saving occurs. The callback function gets passed two parameters: $meta (first param) meta box data array and $post_id (second param). You can also halt saving by returning FALSE.

save_action

'save_action' => 'my_save_action_func' // defaults to NULL

Callback used to execute custom code after saving. The callback function gets passed two parameters: $meta (first param) meta box data array and $post_id (second param).

head_filter

'head_filter' => 'my_head_filter_func' // defaults to NULL

Callback used to insert content into the <head> tag. Also used to override or insert <style> or <script> tags into the head.

head_action

'head_action' => 'my_head_action_func' // defaults to NULL

Callback used to insert content into the <head> tag.

foot_filter

'foot_filter' => 'my_foot_filter_func' // defaults to NULL

Callback used to insert content into the footer. Also used to override or insert <script> tags into the footer.

foot_action

'foot_action' => 'my_foot_action_func' // defaults to NULL

Callback used to insert content into the footer.

Display Options

hide_editor

'hide_editor' => TRUE // defaults to NULL

Used to hide the default content editor in a page or post.

hide_title

'hide_title' => TRUE // defaults to NULL

Used to hide the meta box title.

lock

'lock' => WPALCHEMY_LOCK_TOP // defaults to NULL

Used to lock a meta box in place, possible values are: “top”, “bottom”, “before_post_title”, “after_post_title”, it is recommened that you use the following constants to set the value of this option: WPALCHEMY_LOCK_TOP, WPALCHEMY_LOCK_BOTTOM, WPALCHEMY_LOCK_BEFORE_POST_TITLE, WPALCHEMY_LOCK_AFTER_POST_TITLE.

Using WPALCHEMY_LOCK_BEFORE_POST_TITLE or WPALCHEMY_LOCK_AFTER_POST_TITLE allows you to prevent other meta boxes from being dragged and dropped above or below your custom meta box. This is really useful when you are not using the editor (e.g. its hidden) and you want to have your own custom fields always at the top.

view

'view' => WPALCHEMY_VIEW_ALWAYS_OPENED // defaults to NULL

Used to set the initial view state of the meta box, possible values are: “opened”, “closed”, “always_opened”, it is recommended that you use the follwing constants to set the value of this option: WPALCHEMY_VIEW_START_OPENED, WPALCHEMY_VIEW_START_CLOSED, WPALCHEMY_VIEW_ALWAYS_OPENED.

hide_screen_option

'hide_screen_option' => TRUE // defaults to NULL

Used to hide the show/hide checkbox option from the screen options area.

The Guts of the Meta Box

You probably noticed that the actual contents of the meta box come from the meta.php file. I’ve decided to leave the meta box content definition up to you vs having the class create form fields (this should give a you more freedom to manipulate the meta box contents to your liking).

Lets review the different parts to this solution, first off the meta.php:

[+ show code]

How to Use It!

All of the functions in this class are very WordPress friendly. If you are familiar with the WordPress Loop, you should have no problem using this class.

When working on your meta box template file (meta.php) there are a few variables available to you:

$post; // this is the current post, use $post->ID for the current post ID
$metabox; // this is the meta box helper object
$mb; // same as $metabox, a shortcut instead of writing out $metabox
$meta; // this is the meta data

Lets go over the different functions available in the class and how to use them. Important: you should refer to the meta.php HTML example above by using the line numbers given below. This should help you see the code in action.

the_field($name)

Use this function to set the current working field:

<?php $metabox->the_field('description');

the_name([$name])

This function will print the form field name. If you’ve set the current working field with the_field($name) then you can simply do the following:

$metabox->the_name();

If you have not set the current working field prior to calling this function, you can manually pass in a field’s name:

$metabox->the_name('name');

get_the_name([$name])

Same as the_name([$name]) except that it returns a value instead of printing out the value.

the_value([$name])

Use this function to get the current value of the field.

$metabox->the_value();

If you have not set the current working field prior to calling this function, you can manually pass in a field’s name:

$metabox->the_value('name');

get_the_value([$name])

Same as the_value([$name]) except that it returns a value instead of printing out the value.

if ($metabox->get_the_value() == '_self') echo $selected;

the_index()

Used inside have_fields() and have_fields_and_multi() to print the current index number.

get_the_index()

Same as the_index() except that it returns a value instead of printing out the value.

is_first()

Used inside have_fields() and have_fields_and_multi() to check if the field group is the first field group. When using the_group_open() and the_group_close(), the first HTML element will automatically have a css class of first.

is_last()

Used inside have_fields() and have_fields_and_multi() to check if the field group is the last field group. When using the_group_open() and the_group_close(), the last HTML element will automatically have a css class of last.

is_value([$value])

Uses to check the existence of a value.

<input type="radio" name="<?php $mb->the_name(); ?>" value="admin"<?php echo $mb->is_value('admin')?' checked="checked"':''; ?>/> Admin

The above would be equivalent to:

<input type="radio" name="<?php $mb->the_name(); ?>" value="admin"<?php echo ($mb->get_the_value() == 'admin')?' checked="checked"':''; ?>/> Admin

have_fields($name,$length)

This function is used during a loop. It helps you create multiple instances of a field or group of fields.

while($metabox->have_fields('authors',3))

Inside of the loop you can use the_name() and the_value() to help you define your form fields.

<?php while($metabox->have_fields('authors',3)): ?>
<p>
	<input type="text" name="<?php $metabox->the_name(); ?>" value="<?php $metabox->the_value(); ?>"/>
</p>
<?php endwhile; ?>

The above is equivalent to writing:

<p>
	<input type="text" name="_custom_meta[authors][0]" value="<?php if(!empty($meta['authors'][0])) echo $meta['authors'][0]; ?>"/>
</p>
<p>
	<input type="text" name="_custom_meta[authors][1]" value="<?php if(!empty($meta['authors'][1])) echo $meta['authors'][1]; ?>"/>
</p>
<p>
	<input type="text" name="_custom_meta[authors][2]" value="<?php if(!empty($meta['authors'][2])) echo $meta['authors'][2]; ?>"/>
</p>

Another really cool thing you can do with have_fields($name,$length), is to define field groups like the following representation:

_custom_meta[links][0][title]
_custom_meta[links][0][url]
_custom_meta[links][0][nofollow]
_custom_meta[links][0][target]
<?php while($metabox->have_fields('links',5)): ?>
<p>
	<?php $metabox->the_field('title'); ?>
	<input type="text" name="<?php $metabox->the_name(); ?>" value="<?php $metabox->the_value(); ?>"/>

	<input type="text" name="<?php $metabox->the_name('url'); ?>" value="<?php $metabox->the_value('url'); ?>"/>

	<br/><?php $metabox->the_field('nofollow'); ?>
	<input type="checkbox" name="<?php $metabox->the_name(); ?>" value="1"<?php if ($metabox->get_the_value()) echo ' checked="checked"'; ?>/> Use <code>nofollow</code>

	<?php $selected = ' selected="selected"'; ?>

	<br/><?php $metabox->the_field('target'); ?>
	<select name="<?php $metabox->the_name(); ?>">
	<option value=""></option>
	<option value="_self"<?php if ($metabox->get_the_value() == '_self') echo $selected; ?>>_self</option>
	<option value="_blank"<?php if ($metabox->get_the_value() == '_blank') echo $selected; ?>>_blank</option>
	<option value="_parent"<?php if ($metabox->get_the_value() == '_parent') echo $selected; ?>>_parent</option>
	<option value="_top"<?php if ($metabox->get_the_value() == '_top') echo $selected; ?>>_top</option>
	</select>
</p>
<?php endwhile; ?>

have_fields_and_multi($name[, $options])

This function is similar to have_fields($name,$length). You will notice that it does not use the $length parameter. But it provides additional functionality. It allows you to setup a custom “add” HTML element that can be used to create unlimited copies of your field or field group.

The $options param is an associative array which can contain the following values (each being optional).

$options = array('length' => 3, 'limit' => 10);

length: is the number of field groups to display initially (a minimum of sorts).

limit: is the hard limit at which point the “add new link” will stop working.

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, using the features of the class you can easily setup an “add new link” button which would automatically spawn new instances of the field group you defined. Basically, clicking the “add new link” button again would spawn another field group, and so on.

The following is an example of using this function:

<h4>Documents</h4>

<a style="float:right; margin:0 10px;" href="#" class="dodelete-docs button">Remove All</a>

<p>Add documents to the library by entering in a title, 
URL and selecting a level of access. Upload new documents 
using the "Add Media" box.</p>

<?php while($mb->have_fields_and_multi('docs')): ?>
<?php $mb->the_group_open(); ?>

	<?php $mb->the_field('title'); ?>
	<label>Title and URL</label>
	<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>
	
	<?php $mb->the_field('link'); ?>
	<p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>

	<?php $mb->the_field('access'); ?>
	<p><strong>Access:</strong>
		<input type="radio" name="<?php $mb->the_name(); ?>" value="admin"<?php echo $mb->is_value('admin')?' checked="checked"':''; ?>/> Admin
		<input type="radio" name="<?php $mb->the_name(); ?>" value="editor"<?php echo $mb->is_value('editor')?' checked="checked"':''; ?>/> Editor
		<input type="radio" name="<?php $mb->the_name(); ?>" value="subscriber"<?php echo $mb->is_value('subscriber')?' checked="checked"':''; ?>/> Subscriber
		
		<a href="#" class="button" style="margin-left:10px;" onclick="jQuery(this).siblings().removeAttr('checked'); return false;">Remove Access</a>
		<a href="#" class="dodelete button">Remove Document</a>
	</p>

<?php $mb->the_group_close(); ?>
<?php endwhile; ?>

<p style="margin-bottom:15px; padding-top:5px;"><a href="#" class="docopy-docs button">Add Document</a></p>

the_group_open([$tag])

This function is used to create a container element around your fields, by default the HTML tag used is a DIV. The HTML element will be printed out.

<?php $mb->the_group_open(); ?>

get_the_group_open([$tag])

Same as the_group_open([$tag]) except that it returns a value instead of printing out the value.

the_group_close()

This function is used to close the container element around your fields. The HTML element will be printed out.

<?php $mb->the_group_close(); ?>

get_the_group_close()

Same as the_group_close() except that it returns a value instead of printing out the value.

Creating an “Add” Button

This button is used to create new instances of your field or field group. Adding a docopy-GROUPNAME css class to an HTML element will make it clickable. GROUPNAME is the name you used in the have_fields_and_multi($name) function.

<p><a href="#" class="docopy-docs button">Add Document</a></p>

Creating a “Delete” Button

This button is used to remove and instance of your field or field group. Adding a dodelete css class to an HTML element will make it clickable. This button has to be between the_group_open() and the_group_close().

<a href="#" class="dodelete button">Remove Document</a>

Creating a “Delete All” Button

This button is used to remove all instances of your field or field group. Adding a dodelete-GROUPNAME css class to an HTML element will make it clickable. This button does NOT have to be between the_group_open() and the_group_close(). GROUPNAME is the name you used in the have_fields_and_multi($name) function.

<a href="#" class="dodelete-docs button">Remove All</a>

The “remove” and “remove all” buttons are optional. You can still delete fields by simply deleting their values and clicking the post “Update” button. Important: Adding and removing elements will always require you to click the post “Update” button.

Filter Specific Templates, Categories, Tags and Posts

I often find myself creating meta boxes which apply to a specific template or post. With this class, you to filter your meta boxes a few different ways: by template file, category_id, category name/slug, tag_id, tag name/slug and post_id.

When creating a new WPAlchemy_MetaBox instance the following options are available:

exclude_template
exclude_category_id
exclude_category
exclude_tag_id
exclude_tag
exclude_post_id
include_template
include_category_id
include_category
include_tag_id
include_tag
include_post_id

When assigning values to these options you can assign a value as a single value, an array, or a comma separated list. You will see usage examples below.

You can use as many options to achieve the results you want. Use the exclude options exclusively or the include options exclusively or combine them both for more advanced filtering.

It might be helpful to keep the following in mind: the excluding and including will be processed in the order listed above, excludes are processed first and then includes are processed, additionally templates come before categories which come before tags which come before posts.

Using Exclude Options

The exclude options allow you to exclude a template, category, tag, or post from displaying the meta box.

The following example excludes the “product” template from displaying the meta box, however all other templates will display the meta box.

new WPAlchemy_MetaBox(array
(
	'id' => '_custom_meta',
	'title' => 'My Custom Meta',
	'template' => STYLESHEETPATH . '/custom/meta.php',
	'exclude_template' => 'product.php'
));

Using Include Options

The include options allow you to only include a template, category, tag, or post to display the meta box.

This is different from the exclude option, as only the item that you specify will display the meta box, everything else would be excluded.

In the following example, pages that use the “product” and “press” templates will be the only pages that display the meta box, all other templates would not display the meta box.

new WPAlchemy_MetaBox(array
(
	'id' => '_custom_meta',
	'title' => 'My Custom Meta',
	'template' => STYLESHEETPATH . '/custom/meta.php',
	'include_template' => array('product.php','press.php') // use an array for multiple items
	// 'include_template' => 'product.php,press.php' // comma separated lists work too
));

Using Both Exclude and Include Options

For the most part you will do fine using exclude and include options separately from each other.

When you use exclude and include options together the include option will override any exclude options. Knowing this you can do some advanced filtering.

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' => STYLESHEETPATH . '/custom/meta.php',
	'exclude_template' => array('product.php'),
	'include_post_id' => 85
));

More Exclude and Include Examples

The following filters your meta box to posts which have the “download” tag or is post_id 97

new WPAlchemy_MetaBox(array
(
	'id' => '_custom_meta',
	'title' => 'My Custom Meta',
	'template' => STYLESHEETPATH . '/custom/meta.php',
	'include_tag' => 'download',
	'include_post_id' => 97
));

The following filters your meta box by excluding it from several specific posts, unless those posts have a the “download” tag

new WPAlchemy_MetaBox(array
(
	'id' => '_custom_meta',
	'title' => 'My Custom Meta',
	'template' => STYLESHEETPATH . '/custom/meta.php',
	'exclude_post_id' => array(45,48,56,57,88,91,92,93)
	'include_tag' => 'download'
));

The following filters your meta box to posts that belong to the “WordPress” category

new WPAlchemy_MetaBox(array
(
	'id' => '_custom_meta',
	'title' => 'My Custom Meta',
	'template' => STYLESHEETPATH . '/custom/meta.php',
	'exclude_category' => 'WordPress',
	// 'exclude_category_id' => 12 // using category IDs also work
));

Using The Meta Box Values In Your Template

Using meta box values in your templates is just as straight forward, in your template file you would do something like the following:

// usually needed
global $custom_metabox;

// get the meta data for the current post
$custom_metabox->the_meta();

// set current field, then get value
$custom_metabox->the_field('name');
$custom_metabox->the_value();

// get value directly
$custom_metabox->the_value('description');

// loop a set of fields
while($custom_metabox->have_fields('authors'))
{
	$custom_metabox->the_value();
}

// loop a set of field groups
while($custom_metabox->have_fields('links'))
{
	$custom_metabox->the_value('title');

	$custom_metabox->the_value('url');

	if ($custom_metabox->get_the_value('nofollow')) echo 'is-nofollow';

	$custom_metabox->the_value('target');
}

In the code above, $custom_metabox is the object variable you created in functions.php when you used new WPAlchemy_MetaBox.

Again, you are not limited to having to use the class functions, you can use the default WordPress function get_post_meta() as such:

// instead of using helper functions, you can also use ...
$meta = get_post_meta(get_the_ID(), $custom_metabox->get_the_id(), TRUE);

// or ... (same as above)
// $meta = $custom_metabox->the_meta();

echo $meta['name'];
echo $meta['description'];

foreach ($meta['authors'] as $author)
{
	echo $author;
}

foreach ($meta['links'] as $link)
{
	echo $link['title'];
	echo $link['url'];
	if ($link['nofollow']) echo 'is-nofollow';
	echo $link['target'];
}

Download

WPAlchemy MetaBox Class, this project is on github

Contribute

Are you using this class? If so, become a contributor to it’s growth and stability. If you have problems using it, definitely let me know (I like to write and use stable code). Equally, if you have ideas to make it even better, I want to hear that too.

965 responses to “WPAlchemy MetaBox PHP Class”

  1. MattStrange

    Hi Dimas,

    Ive been playing around with the jQuery Datepicker and WPAlchemy but still having problems getting it to work :(

    I thought maybe the jQuery was conflicting with my other plugins so i started a fresh install of WordPress 3.3 and created a very minimum theme to test the datepicker in the custom fields, still doesn’t work and i’m lost for ideas.

    If your not too busy could you take a peak at the source files:

    http://www.lifeofstrange.com/demotheme.zip

    If anybody has a solution for this i would be grateful for your response.

    Kind Regards
    Matt

  2. Patrick

    Conditional Syntax for beginner

    Can anyone help me with code for my template that will replace what I already have for custom fields? I have it pasted below, basically the theme finds if there is an entry for the custom field and then outputs everything after the last line. I would like to do that but with the custom meta boxes.

    Here is the code to replace:

    ID, "Link", true))) {
    echo '
    Link
    ID, "Link", true);
    echo '" target="blank">';
    echo 'Visit this link →';
    echo '
    ';}?>

  3. Francis

    Hi Dimas,

    I tried with this code but does not work neither. I searched on the web and it seems that do_shortcode does not work [embed] outside of the post editor:

    http://www.deluxeblogtips.com/2011/11/show-youtube-video-outside-post.html

    I tried then with wp_oembed_get(). I did a example with a real YouTube URL and it worked but when I try to implement WPalchemy code, it does not work :-/

    https://gist.github.com/1493993

  4. Sean Stiller

    Hi Dimas,

    Thanks again for the amazing WPAlchemy!

    I’ve got a quick question regarding conditional statements. What I’m trying to do is this:

    In a multi field, setup a field with select options. Depending on which option is chosen, I’d like to be able to show certain types of content. This is how I have it outputted at the moment:

    the_meta(); while($highlevelcolumns_metabox->have_fields(‘columns’)) { ?>

    get_the_value(‘twocolumnrow’)) { ?>
    /* Conditional Content Shows Up */
    get_the_value(‘threecolumnrow’)) { ?>
    /* Conditional Content Shows Up */

    And so on for a few more options.. Here’s the back-end code:

    have_fields_and_multi(‘columns’)): ?>
    the_group_open(); ?>

    Select Column Type:

    the_field(‘columntype’); ?>
    <select name="the_name(); ?>”>
    Select…
    <option value="twocolumnrow"the_select_state(‘twocolumnrow’); ?>>Two Column
    <option value="threecolumnrow"the_select_state(‘threecolumnrow’); ?>>Three Column
    <option value="fourcolumnrow"the_select_state(‘fourcolumnrow’); ?>>Four Column
    <option value="sixcolumnrow"the_select_state(‘sixcolumnrow’); ?>>Six Column

    Remove Document

    the_group_close(); ?>

    If you have any thoughts that would be great! I’m sure it’s an easy fix but I’m going crazy trying to figure this out!

  5. Francis

    Hi Dimas,

    It works now, I had to use get_the_value and not only the_value.

    Thanks for your help :)

  6. Patrick

    I will simplify my question. I want to use the wpalchemy-metabox instead of custom fields. Right now I have the following code:

    ?php if((get_post_meta($post->ID, "Link", true))) {
    echo (THE CONDITIONAL CONTENT)')...

    I would like to replace this, but how can I? Let’s say I am using the ‘title’ metabox from the example given. Can someone tell me how to implement that into my theme as done above (conditionally)?

  7. Eduardo

    Hello!

    Firstly, thank you for your hard work. The code is great and works like a charm. I’m just having some problems with the output. I made a custom query to make a loop that shows only posts with a specific meta value. It works when i use the simple custom field solution, but i care about the user experience so i’m using you code. The problem is that it output something like “a:1:{s:9:”EventDate”;s:10:”19/12/2011″;}” and my query can’t find it =[. Have any ideia that could help me?

    Thank you

  8. Basti

    Eduardo, you could use WP_ALCHEMY_MODE_EXTRACT to store the values as unique custom fields instead of bundling them. I guess this is the only way to be able to query for posts by meta_value.
    Another way would be to create a save filter which stores the value into a unique field, but it makes only sense if you have a metabox with many fields and you don’t want to store all values into unique fields.

  9. Deryk Wenaus

    any thought of integrating your excellent class with what will be coming in wordpress 3.4? http://core.trac.wordpress.org/ticket/18179

    I’m guessing that your class might be overkill, but at least it can inform the new WP_Meta_Box class in development, because your class is the most mature one out there. Or even better, a stripped down version of your class can become the backbone of the new class. like woo menus did for the menu system.

  10. Moshe

    Hi there,

    I was wondering if it is possible to create a repeatable meta-box like you see in this video: http://www.screenr.com/a17s [starts at about 40 seconds]. If so, any suggestions on how?

    Thanks,

    Moshe

  11. Brokenflipside

    I am working on a very similar issue as @MattStrange, I am trying to dynamically add fields with jquery datepicker (and another field filtering out non-numbers) but the problem seems to be that the script is only run on the first fields not the dynamically generated ones. Any ideas how I might overcome this issue?

  12. Kirsten

    Thanks again for such a wonderful script and for your help last month. I’m working on another part of my site and am attempting to implement a repeatable dropdown/select field using WPAlchemy. I can get the repeatable fields to function and save properly when I hard-code the select options (https://gist.github.com/1561986). However, when I try to generate the options using a WPQuery, the repeatable fields function properly within the admin (i.e. I can see the dropdowns and can add new fields) however they are not saving when I update the post. I’ve posted the non-working code for my meta.php here: https://gist.github.com/1561943

    Not sure where to start investigating what might be causing this, so appreciate any pointers you might have.

    Thanks!

  13. Frances

    Hi Dimas,

    I think you mentioned a couple of months ago that you were working on nested repeating fields (i.e. a repeating field in a repeating field) – but I can’t find the comment now. Will you be adding this to WPAlchemy, or may I request this?

    Also, with MediaAccess, is there a way to get more than just the URL? For example, if you have an image with a caption, it wraps the image and link in the ‘caption’ shortcode. Is it possible to grab that data?

    Ciao,

    Frances

  14. Kirsten

    I also just noticed a rather strange behavior when I try to display items from a repeating field metabox. I have a grouped set of links (with a url and title field) and it appears that the first item (in this case a website link) does not display, but all of the other items in the array do. If I enter test data where I have Link One and Link Two, the list of links will start with Link Two and Link One is nowhere to be found!

    I’ve tried changing the mode to both EXTRACT or ARRAY or none, but that did not fix the issue. I’ve also tried copying the exact code that you list above for have_fields_and_multi in case I’d changed something when I customized the code, but, again, no luck.

  15. bazel

    using WPALCHEMY_MODE_EXTRACT and it works fine if I add value from the meta box field but If I add from a custom field its not showing in the meta box. any help? thanks

  16. IndyJones

    First….awesome job! Second, I was curious if I was perhaps doing something wrong using the MetaBox with a custom template. It appears that the metabox only shows if the template is changed, then saved/updated. I *thought* I had purchased a theme at one point where this appeared/disappeared when the template was changed.

    Ideally it would be great (provided I’m not doing something wrong) to have the metabox appear/disappear based upon which template is selected in the drop down without having to save/update.

    Doug.

  17. brokenflipside

    I have solved my issue of adding script to dynamically rendered fields by using .live (which I realize there are better methods, but I will look into that later.)

    http://pastebin.com/UZXpf4LX

    Now that I have all my repeating fields working properly I am working on a way to cherry pick data from each set of repeating fields.

    Basically the admin can add tracks to my CPT ‘Album’ and I want my sidebar widget to show 1 track and some of its associated information from 1 album randomly.

    In one of the video walkthrough Dimas mentions possibly adding a way to use MODE_EXTRACT on secondary tiers, but I don’t think that has made it in yet. Has it?

    Any ideas are greatly appreciated!

  18. Brokenflipside

    Update: So I solved my issue of cherry picking fields of data by using jQuery to apply a count to all the divs labeled “trackInfo” and then randomly pull all the output inside those divs. (essentially)

    This is a quick solution for me and won’t really effect the site much as there would never be more than 20 tracks to choose from, but realistically I would prefer a server side solution, more to the point, if WPALCHEMY_MODE_EXTRACT worked on secondary fields it would be much more effective than a JS solution.

  19. Brokenflipside

    @Dimas, my custom post type is Albums.

    Each album has my custom metabox (using your fantabulous class) to add the albums’ tracks. Each row includes 5 fields. This is the group.

    I want to be able to grab 1 track and some of the corresponding info to display in a widget.

    I accomplished this by loading all the tracks on every page refresh in a hidden div, then using jQuery grab the appropriate information and display a random set.

    Using ajax would certainly do the trick, of course I would have to determine how to parse the array to only grab 1 track at a time.

    You mentioned in your wpalchemy-metabox-data-storage-modes video that in the future you might add the ability to apply WPALCHEMY_MODE_EXTRACT to secondary tiered data, which I took to mean giving fields inside have_fields_and_multi they’re own set of data/metakey without grouping it all in a serial array.

    Does that make anymore sense? None the less I have a working solution, if anyone needs info (assuming that made sense, just ask!)

  20. sagive

    Hey.. First thing. thanks a lot – i love alchemy!! :)

    Got a little problem… When ever i use this:
    $clients->the_value('client_url')

    i get the text value – no echo needed….
    how can i assign that to a variable.

    when i try to do this:
    $var = $clients->the_value('client_url');

    it still get echoed on screen.

  21. Kirsten

    @sagive you’ll want to us get_the_value(); instead of the_value(); in order to have it store the value instead of echoing.

  22. sagive

    of course :) – thanks @Kirsten