Skip to content

Commit

Permalink
Added support for select elements when creating a metabox
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Oct 7, 2011
1 parent e474809 commit 187d8e0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
33 changes: 29 additions & 4 deletions jw_custom_posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,34 @@ function($post, $data) { // function that displays the form fields

// Get the saved field values
$meta = get_post_custom($post->ID);

// For each form field specified, we need to create the necessary markup
// $name = Label, $type = the type of input to create
foreach ($inputs as $name => $type) {
// Will turn "Movie Title" into snippet_info_movie_title
#'Happiness Info' in 'Snippet Info' box becomes
# snippet_info_happiness_level
$id_name = $data['id'] . '_' . strtolower(str_replace(' ', '_', $name));

if( is_array($inputs[$name]) ) {
// then it must be a select
// filter through them, and create options
$select = "<select name='$id_name' class='widefat'>";
foreach ($inputs[$name][1] as $option) {
// if what's stored in the db is equal to the
// current value in the foreach, that should
// be the selected one

if ( isset($meta[$id_name]) && $meta[$id_name][0] == $option) {
$set_selected = "selected='selected'";
} else $set_selected = '';

$select .= "<option value='$option' $set_selected> $option </option>";
}
$select .= "</select>";
array_push($_SESSION['taxonomy_data'], 'select_difficulty');
} else {
$select = '<select><option>--</option></select>';
}

// Attempt to set the value of the input, based on what's saved in the db.
$value = isset($meta[$id_name][0]) ? $meta[$id_name][0] : '';
Expand All @@ -200,12 +222,15 @@ function($post, $data) { // function that displays the form fields
// TODO - Add the other input types.
$lookup = array(
"text" => "<input type='text' name='$id_name' value='$value' class='widefat' />",
"textarea" => "<textarea name='$id_name' class='widefat' rows='10'>$value</textarea>"
"textarea" => "<textarea name='$id_name' class='widefat' rows='10'>$value</textarea>",
"select" => $select
);
?>

<p>
<label><?php echo ucwords($name) . ':'; ?></label><br />
<?php echo $lookup[$type]; ?>
<?php echo $lookup[is_array($type) ? $type[0] : $type]; ?>

</p>
<?php
}
Expand Down
14 changes: 13 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defaults. For example, if I only want to provide support for a title and an
excerpt, I could do:

$snippet = new JW_Post_Type('Snippet', array(
'supports' => array('title, 'excerpt')
'supports' => array('title', 'excerpt')
);

### Custom Taxonomies
Expand Down Expand Up @@ -51,3 +51,15 @@ the snippet, etc.

Within the second array argument, set the label text and the type of input to
display,respectively.

However, if you require a select box, you need to pass an array, with the first
key equaling the type of input to create ('select' element), and the second key
being an array of choices. For example:

$snippet->add_meta_box(
'Favorite Food' =>
array(
'select', // type of form field
array('pizza', 'tacos', 'sandwiches') // list of options
)
);

0 comments on commit 187d8e0

Please sign in to comment.