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. Chris

    First of all… Amazing explanation, this is going to solve a great deal of issues we have been having in simplifying the deployment of these metaboxes.

    I have a few questions though which I am hoping you would be able to address or assist in resolving.

    1) A problem we always run into even with wordpress 3.0 is the ability to show a metabox which allows you to show all the images and file attachments associated with a post. If you could somehow extend your plugin which would allow one to include and style the display of images it would be very very helpful (possibly using timthum to show the thumbnails of such images all with the same size). I am assuming this is probable a very simple addition (when dealing with images) but what would be excellent is if it would automatically also separate out any other files which might have been uploaded (again with the ability to style these). On the front end we have use the plugin attachment xtender which I remember worked fairly well.

    2) As a separate point but expanding on the above request, I feel it would be highly valuable if the media upload capabilities could be somehow simplified while still using the word press media upload database structure. I personally find the default uploaded to be far to complicated for many users I have shown it to. My suggestion is a simple upload button one can add to a metabox with an optional URL input field… Clicking on the upload button would bring up your file browser where you can select one/more file and click save, this would then automatically upload each one through ajax with a progress bar online with the page, present a thumbnail and if defined in the metabox the applicable wordpress mediafields associated with that image. Now, if someone wants to modify an image such as cropping he can click the image or edit link which would bring up the standard wordpress media for that image. My logic here is that in nearly every situation this would be the absolute best way of doing things and your code here for meta boxes would allow so many individuals customize it to meet their needs.

    3) These next few items fall outside the scope of your code a bit but I am hoping you have a solution for me and others as I have spent hours trying to resolve them.

    A- I have been trying to find a way to directly influence the location of post meta boxes. What We have been looking for is a way to force the position of a meta box into a default location other than just priority of high. Additionally, if we were able to disable moving a meta box would be very helpful and being able to tell wordpress to reset the location of a meta box if it was moved by a user. On the same level I don’t know of a way to place a custom meta box above or directly below the default wordpress title.

    B- do you know of a way to directly order (or reorder) the wordpress default menu items. Example- Through post types you can of course create a new admin menu but what I am unable to do is place such a menu item above the dashboard or for that matter change the location of the dashboard. There are plugins which allow this but I am hoping there is some simple code I can include in the functions file.

    B-

  2. Scott Lee

    First off, thank you! This PHP class helper is awesome, and greatly makes custom field creation easy! So thank you!

    I do have a question concerning child themes. What can be done so that the required files/folders for WPAlchemy could be placed inside the child theme rather than the parent?

    For instance I’m using Hybrid and if I want to use WPAlchemy I have to place the files in the Hybrid parent folder. I can still edit the functions.php file within the child to “activate” WPAlchemy. Though the meta files are in the parent theme folder.

    Any ideas?

  3. Scott Lee

    Great idea, thank you!

    I decided to swap out TEMPLATEPATH with STYLESHEETPATH since I’m dealing with child themes.

    Thanks again for the quick response.

  4. Scott Lee

    Hopefully this is the last question you’ll get from me. :)

    Still concering child themes. Since the power of child themes lies in not touching the parent theme files. How do I go about calling field values from within the child themes functions.php? While true I could create a page.php in the child directory and call the value (ex. $custom_metabox->the_value(‘description’);) from there. But the child page.php would overwrite the parent’s page.php, which in my opinion defeats the purpose of a child themes.

    All that to say, I’m trying to use an action hook to call the values directly from the child’s functions.php and I’m getting the following errer: Fatal error: Call to a member function the_value() on a non-object

  5. Scott Lee

    Never mind, I’m an idiot. I was calling the value too soon. Like before the Loop too soon.

  6. Chris

    Thank you very much for your quick reply.

    I greatly appreciate your suggestions on these topics.

    Given your interest in additional features that might be needed here are some others which I have noticed although again they might not be related to this exact meta box functionality.

    1) An issue which I recently ran into was that I wanted to create a custom taxonomy/meta box for “article source”. My objective was to essentially have a custom taxonomy were the items within it were essentially entries for different source publications (ie – The Washington Post). I goal was essentially very basic… just giving the editor the ability to pick from a drop down list of source publications so he could define the source. As I am assuming you are aware this is totally possible… the problem I ran into was that it seems I am unable to assign a meta box or additional custom field to a taxonomy. The goal here was to be able to assign additional fields for each taxonomy entry so I could for example add an image to each article source and have the site admin manage such sources all from one location. Do you think you can somehow modify your plugin to allow for this to happen? (Possibly remove the taxonomy description field and essentially have meta box values stores in there to use the default database structure)?

    2) One area I got confused about related to the new exact and array storage functions you described. Recently I ran into an issue in which I wanted to use wordpress for a real estate site. The problems came into play when I was using custom fields to store property related attributes and then tried to create a public search which would query such entries. The problem was that it took very long to query the system ( I am assuming because the meta keys/values are not indexed individually)… So, my question here is does your solution somehow resolve this problem? If so, what are the side effects of your new storage system or when would it make sense to use your way?).

    3) This is a big one: I find it is very difficult for users to develop custom meta boxes which include things such as data/time pickers. I feel it would be VERY VERY helpful to develop some type of example class for all different types of select/input meta box types. A key problem relates to how jquery or motools libraries are added without messing up other elements. In my example all I wanted to do is create an event calendar and found it very difficult to create the date/time calendar using motools as the date/time selections (to ensure fields are entered correctly).

    Regarding your previous comments, would you potentially be willing to assist me on the development of such a media upload meta box if I could contribute some funds towards your developments? Please contact me by email when you have a moment.

  7. nicolas

    Hi dimas.
    I’have created a custom post type “A” with a custom meta “A-meta”.
    I have a pb:
    I want to display the list of all “A-meta” values but not especially in the specific custom” post type A page ” .

    Here’s the error :Call to a member function the_meta() on a non-object in on line

    It’s works good on ” post type A page ”

    is their a way to do it.I’ve tryed with a function but it don’t work here.

  8. Nicolas

    ok thanks!
    it’s works with:

    global $custom_metabox3;
    $custom_metabox3->the_meta();
    
  9. Scott Lee

    Thank you Dimas! It worked brilliantly.

  10. micha

    first of all: super cool idea!
    but i think i’ve found a bug: try your meta.php and check some checkboxes there, you’ll find that on relaod they are differently checked.

    i’ve made a very simple example:
    project-meta.php:

    // $clients is ann array of another custom post-types...
    while($metabox->have_fields('clients', count($clients)) ): ?>
    	get_the_index()]->post_title; ?>
    		
    	<input type="checkbox" name="the_name(); ?>" value="1" get_the_value()) echo 'checked="checked"'; ?> />
    

    let’s say i have 2 clients there, if you check the second and reload the admin-page, the first gets checked, not the second.
    if you check both, both stay checked.

  11. micha


    $clients[$mb->get_the_index()]; ?>

    that gives an ugly 500 ;-) i now use

    get_the_index()]->post_title; ?>

    which is sub-optimal too but at least it works for testing purposes …

  12. micha

    ok i worked around that, but have found another small bug:

    i’m currently fiddling around with multiple-fields in this case phone-numbers.
    i copied the have_fields_and_multi() part and do have now:

    have_fields_and_multi('phones')): ?>
    the_group_open(); ?>

    phone get_the_index()+1); ?>
    <input type="text" name="the_name(); ?>" value="the_value(); ?>"/>
    get_the_index() > 0 ) : ?>
    delete

    the_group_close(); ?>

    new phone

    delete all


    but after deleteing all phone-numbers the array count doesn’t get reset, i still have for example 4 as the_index.
    it’s just a small glitch no big deal.

  13. josh

    I was reading about how you can include specific meta boxes based on the category in which the post resides… However, my question is this:

    Will the filters run on the meta box(es) as soon as the user clicks the checkbox for a particular category, or only after the user saves the post in that category?

  14. Josh

    Thanks, that’s kinda what I figured… I suppose I could just let the client know that they’ll need to click “Update/Publish” after choosing the appropriate category in order to see what sorts of custom options are available for that particular post.

  15. Josh

    I’m not sure I like the 1st idea either really… The less markup (visible or hidden), the better. (at least in my book)

    As for your second idea, I really like that one. I know me personally, when I write a post in WordPress I don’t normally select the category and/or tags until AFTER I’ve already written the post, added the excerpt, given it a title, etc…

    So if other people do the same, then it might be a very useful idea to display an alert letting them know that there are more options now available for them to fill out.

  16. Josh

    I just took a look at the “Select Category” section in the WordPress write page, and it looks like each of the checkboxes are given unique id’s like so: in-category-X where X = the category ID…

    So I’d imagine it wouldn’t be too terribly hard to run a foreach loop and get all available category ID’s, then enqueue a JS file which has onclick events to run the filters.

1 2 3 39