Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is the code I used to create the meta boxes:

<?php

$key = "project";
$meta_boxes = array(
    "project_services" => array(
        "name" => "project_services",
        "title" => "Services",
        "description" => "List the services provided for the project."),
    "project_name" => array(
        "name" => "project_name",
        "title" => "Name",
        "description" => "Write the name of the project."),
    "project_overview" => array(
        "name" => "project_overview",
        "title" => "Overview",
        "description" => "Write an overview of the project.")
    );

function create_meta_box() {
    global $key;

    if( function_exists( 'add_meta_box' ) ) {
        add_meta_box( 'new-meta-boxes', ucfirst( $key ) . ' Description', 'display_meta_box', 'post', 'normal', 'high' );
    }
}

function display_meta_box() {
    global $post, $meta_boxes, $key;
?>

<div class="form-wrap">

<?php
    wp_nonce_field( plugin_basename( __FILE__ ), $key . '_wpnonce', false, true );

    foreach($meta_boxes as $meta_box) {
        $data = get_post_meta($post->ID, $key, true);
?>

<div class="form-field form-required">
<label for="<?php echo $meta_box[ 'name' ]; ?>"><?php echo $meta_box[ 'title' ]; ?></label>
<input type="text" name="<?php echo $meta_box[ 'name' ]; ?>" value="<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>" />
<p><?php echo $meta_box[ 'description' ]; ?></p>
</div>

<?php } ?>

</div>
<?php
}

function save_meta_box( $post_id ) {
    global $post, $meta_boxes, $key;

    foreach( $meta_boxes as $meta_box ) {
        $data[ $meta_box[ 'name' ] ] = $_POST[ $meta_box[ 'name' ] ];
    }

    if ( !wp_verify_nonce( $_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__) ) )
        return $post_id;

    if ( !current_user_can( 'edit_post', $post_id ))
        return $post_id;

    update_post_meta( $post_id, $key, $data );
}

add_action( 'admin_menu', 'create_meta_box' );
add_action( 'save_post', 'save_meta_box' );

?>

Then, I changed the input to a textarea:

<textarea name="<?php echo $meta_box[ 'name' ]; ?>">
<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>
</textarea>

This turns all the three fields into text areas. I would like to only make the third field, project_overview a textarea. Any suggestions?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Modify your array definition with something like this to add a new element of the array that specify the type of the field

"project_overview" => array(
    "name" => "project_overview",
    "title" => "Overview",
    "description" => "Write an overview of the project.",
    "type"=>"textarea")

Then in your code substitute

<input type="text" name="<?php echo $meta_box[ 'name' ]; ?>" value="<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>" />

with something like this

<? if ( $meta_box['type'] == 'textarea') { ?
   <textarea name="<?php echo $meta_box[ 'name' ]; ?>">
   <?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>
   </textarea> 
} 
else  { ?>
   <input type="text" name="<?php echo $meta_box[ 'name' ]; ?>" 
   value="<?php echo      htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>" />
} ?>

Is better that you wrap the piece of code above in a function, like

function print_meta_box ( $meta_box ) {
  switch (meta_box ['type'] ) {
      case 'textarea':
            .... your code ...
            break;
      default:
  }  
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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