Pergunta

I'm writing a plugin which creates a custom post_type called "dictionary_entry" which has several custom meta boxes and fields. I'd like to add an addition field which allows the custom post author to upload an audio clip.

I've done some digging and tried the code offered here but I can't get it to work.

I think one possible answer to my question would be the "type" parameter for fields. I've seen "text", "textarea", "time", "color", "radio", etc. but I haven't been able to find a list of all the possibilities. Is it wishful thinking that there might be a field type: "file" or "upload"?

I'm going to skip the code for adding the custom post_type, but here's an example of my code for adding the meta boxes (in case somebody else is trying to use this, remember to use your custom post_type in the 'pages' parameter):

//meta box code

$meta_boxes = array();

$meta_boxes[] = array(
'id' => 'examples',                         // meta box         id, unique per meta box
'title' => 'Examples',          // meta box title
'pages' => array('dictionary_entry'),   // post types, accept custom post types as well, default is array('post'); optional
'context' => 'normal',                      // where the meta box appear: normal (default), advanced, side; optional
'priority' => 'high',                       // order of meta box: high (default), low; optional

'fields' => array(                          // list of meta fields

    array(
        'name' => 'Example 1',                  // field name
        'desc' => 'Use it in a sentence? EX: Kanien\'kéha kahrónkha.',  // field description, optional
        'id' => $prefix . 'example1',               // field id, i.e. the meta key
        'type' => 'text',                       // text box
        'std' => '',                    // default value, optional
        'validate_func' => 'check_apos'         // validate function, created below, inside RW_Meta_Box_Validate class
    ),

    array(
        'name' => 'Translation 1',                  // field name
        'desc' => 'What does the sentence mean? EX: I speak Mohawk.',   // field description, optional
        'id' => $prefix . 'ex_translation1',                // field id, i.e. the meta key
        'type' => 'text',                       // text box
        'std' => '',                    // default value, optional
        'validate_func' => 'check_apos'         // validate function, created below, inside RW_Meta_Box_Validate class
    )
)
);

foreach ($meta_boxes as $metabox) {
add_meta_box... //see the codex for add_meta_box()
}
Foi útil?

Solução

I figured out how to do this by digging into the code found here. If you take a look, you'll recognize parts of my code quoted above. I was originally using this class, but didn't fully realize it. It's a custom class which can be called to add various meta boxes / fields.

It turns out that the "type" parameter I was wondering about actually belongs to this Class (as opposed to the Wordpress API) and that it does allow for a type: 'file' which brings up a default file picker window (not the built-in media uploader). For my purposes this is okay, because I don't need all the slick options.

If you're reading this, you've probably already googled this question and seen a wide variety of posts that partially explain how to do this. For what it's worth, I found this to be the easiest way to add this functionality that ALSO works for custom post_types (without a fair amount of hacking). I hope this is useful to someone else.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top