4

Here's the scenario:

In my plugin, I want to have a metabox show up on the post / page editor.

I want the metabox to be -exactly- like WordPress' 'Custom Fields' metabox, ( Add another, delete, update, auto-populated dropdown, Enter new link, AJAX magic, etc ); with a couple minor differences:

  • Instead of just 'Name' and 'Value', I want 'Target', 'Name' and 'Value'

  • Instead of saving them all as 'public' custom fields that automatically appear inside the WP Custom Fields metabox, I want them all stored as a single multidimensional array inside a 'private', namespaced, field...i.e., _myplugin_custom_fields = array( 1 => array( 'target' => 'mytarget1', 'name' => 'myname1', 'value' => 'myvalue1' ), 2 => array( 'target' => 'mytarget2', 'name' => 'myname2', 'value' => 'myvalue2' ) )

I've spent all day digging through the tangled web of WP Core, and have not been able to find a straightforward way to pull this off. If I move forward based on my current understanding, I'll end up duplicating hundreds of lines of core code from numerous php and js files.

Can anyone here in WordPress Answers-land help point me in the right direction? I have plenty of experience in adding and leveraging metaboxes within themes and plugins, but this one has me totally stumped.

Please speak up if I'm being unclear.

Thanks in advance to all who reply!

3 Answers 3

2

The short answer is that there isn't any straightforward way to do this, since the custom field metabox was not written with extensibility in mind.

You could add the additional field using JavaScript and hijack the Add button to send the data to your custom AJAX handler that would store them as you want.

0

I'm saving my custom fields with the name _meta_whatever. Starting the name with an underscore, stops WordPress showing up the custom fields in the dropdown-box in the normal posts.

For saving in an array I'm using HTML-code like this.

...
<input type="text" class="short" name="_events_meta[startDate]" id="_events_meta[startDate]" value="<?php if(!empty($meta['startDate'])) echo $meta['startDate']; ?>"/>
<input type="text" class="extrashort" name="_events_meta[startTime]" id="_events_meta[startTime]" value="<?php if(!empty($meta['startTime'])) echo $meta['startTime']; ?>"/>
...

For retrieving the meta-values, this is my code I'm using

...
$meta = get_post_meta($post->ID,'_events_meta',TRUE);
...

Now I have an assoziative array $meta where the key name is given in the brackets in the id-Attribute of the input-Element.

For saving I'm using code like this

...
$new_data = $_POST['_events_meta'];
...
add_post_meta($post_id,'_events_meta',$new_data,TRUE);
...

So it is saved as one record in wp_postsmeta

Hope this helps and is clearly expressed from my side?

2
  • Thanks for the input, but it's not quite what I'm looking for. I'm familiar with how to do that bit (as shown in my second bullet point), but the main thing I'm after here is how to basically duplicate the Custom Fields Metabox -AND- add a 3rd Field (Target) -AND-, after duplicating the Custom Fields Metabox, stuffing all my values into an array. I think that if I can figure out how to handle the first bullet point, I'll be able to pretty easily determine how to organize my array. Thanks again for taking the time to contribute! Commented Mar 2, 2011 at 20:05
  • @SethMerrick: Excuse for misunterstanding your question. Now I unterstand, but it is a really hard way to go ;-)
    – Guru 2.0
    Commented Mar 2, 2011 at 20:09
0

Sounds like we are working on similar things. Not sure if this will help but may give you some other ideas.

1st of three part blog post - http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.