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.

I have the following custom function code which i'm using to create custom fields for a post type.

The issues is that I need the custom fields to show up individually and at the moment they just show up as 'key' which means that when i try to link a form field (Gravity Forms) to a custom field, I can only link to the 'key' field.

How can this be modified to show each custom field individually?

/* Custom Fields */
$key = "key";
$meta_boxes = array(
"cf_link" => array(
"name" => "cf_link",
"title" => "URL/Link",
"description" => "Enter the URL/Link here."),
"cf_image" => array(
"name" => "cf_image",
"title" => "Image",
"description" => "Enter the image URL here."),
"cf_instructions" => array(
"name" => "cf_instructions",
"title" => "Instructions",
"type" => "textarea",
"description" => "Enter any specific instructions here.")
);

function create_meta_box() {
global $key;

if( function_exists( 'add_meta_box' ) ) {
add_meta_box( 'new-meta-boxes', ucfirst( $key ) . 'Product Details', 'display_meta_box', 'products', '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>

<?php if( $meta_box['type'] === 'textarea' ) { ?>
  <textarea name="<?php echo $meta_box[ 'name' ];?>" rows="4"><?php echo htmlspecialchars($data[$meta_box['name']]); ?></textarea> 
<?php } else { ?>
  <input type="text" name="<?php echo $meta_box['name']; ?>"
      value="<?php echo htmlspecialchars( $data[$meta_box['name']]); ?>" /><?php }?>
<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' );
share|improve this question

closed as off-topic by Mark Kaplun, TheDeadMedic, birgire, Pieter Goosen, Johannes Pille Sep 4 at 6:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Mark Kaplun, TheDeadMedic, birgire, Pieter Goosen, Johannes Pille
If this question can be reworded to fit the rules in the help center, please edit the question.

    
This may be a better question for the folks at gravityhelp.com they are always able to help solve my gravity form issues. –  BandonRandon Dec 9 '11 at 21:53

1 Answer 1

With some help, I have managed to modify the code so that Gravity Forms now picks up the Custom Fields.

As the original code is for a serialized array of field information and I don't believe Gravity Forms can utilize this original code so here is the new code for individual row creation for each Custom Field (which GF can use):

/* Custom Fields */
$key = "key";
$meta_boxes = array(
"cf_link" => array(
"name" => "cf_link",
"title" => "URL/Link",
"description" => "Enter the URL/Link here."),
"cf_image" => array(
"name" => "cf_image",
"title" => "Image",
"description" => "Enter the image URL here."),
"cf_instructions" => array(
"name" => "cf_instructions",
"title" => "Instructions",
"type" => "textarea",
"description" => "Enter any specific instructions here.")
);

function create_meta_box() {
global $key;

if( function_exists( 'add_meta_box' ) ) {
add_meta_box( 'new-meta-boxes', ucfirst( $key ) . 'Product Details', 'display_meta_box', 'products', '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, $meta_box['name'], true);
?>
    <div class="form-field form-required">      
    <label for="<?php echo $meta_box[ 'name' ]; ?>"><?php echo $meta_box[ 'title' ]; ?></label>
<?php if( $meta_box['type'] === 'textarea' ) { ?>
  <textarea name="<?php echo $meta_box[ 'name' ];?>" rows="4"><?php echo htmlspecialchars($data); ?></textarea> 

<?php } else { ?>

  <input type="text" name="<?php echo $meta_box['name']; ?>"
      value="<?php echo htmlspecialchars($data); ?>" /><?php }?>
<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;

foreach ($data as $d => $v) {
        update_post_meta( $post_id, $d, $v );
    }
}

add_action( 'admin_menu', 'create_meta_box' );
add_action( 'save_post', 'save_meta_box' );
share|improve this answer

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