Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$15
Custom Field Function
This works fine when editing an individual post and each custom field can be edited and saved correctly.
I am also using Gravity Forms to build a front-end submission form for the custom post type. I am trying to map fields from the form to the custom fields, but at the moment they simply show up as one field 'key', and as a result they cannot be populated or saved correctly.
How can this function be modified to show each custom field individually so that the form fields can be mapped to each field?
/* 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' );
This question has been answered.
robster | 12/10/11 at 1:19pm
Edit
(2) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
12/10/11
1:49pmMaor Barazany says:Currently you are saving all custom fields as one serialized array, which is better practice.
You might see how gravity forms handles this and may have input texts in the form to save an array,
something like
<input type="text" id=data['key'] />
But I don't know if gravity forms supports this.
If you would want to change your metaboxes to save each field as a different key in the db you can use this:
In your code, replace
update_post_meta( $post_id, $key, $data );
With this -
foreach ($data as $d => $v) {
update_post_meta( $post_id, $d, $v );
}
and also replace your display_meta_box function with this
$data[ $meta_box[ 'name' ] ] = $_POST[ $meta_box[ 'name' ] ];
With this -
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
}
- 12/11/11 8:48am
robster says:Thanks Maor this works brilliantly! I presume this could get a bit load heavy for the DB in time depending on the amount of inputs/fields, but will certainly work for now!
- 12/11/11 8:48am
-

Last edited:
12/10/11
1:50pmSébastien | French WordpressDesigner says:The name of your fields (in the gavity form) are well cf_link, cf_image, and cf_instructions ?
This question has expired.
Gabriel Reguly, John Cotton, Julio Potier, Luis Abarca, Francisco Javier Carazo Gil, robster voted on this question.
Current status of this question: Completed
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
