Custom Field & Gravity Forms

时间:2011-12-09 作者:Rob

我有以下自定义函数代码,用于为帖子类型创建自定义字段。

问题是,我需要自定义字段单独显示,此时它们只显示为“键”,这意味着当我尝试将表单字段(重力表单)链接到自定义字段时,我只能链接到“键”字段。

如何修改此选项以单独显示每个自定义字段?

/* 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\' );

1 个回复
SO网友:Rob

在一些帮助下,我成功地修改了代码,以便Gravity表单现在可以拾取自定义字段。

由于原始代码用于字段信息的序列化数组,我认为Gravity表单无法使用此原始代码,因此下面是为每个自定义字段创建单独行的新代码(GF可以使用):

/* 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\' );

结束

相关推荐

自定义Metabox字段帮助提示弹出窗口

是否有任何内置到核心的东西显示一点(?)在翻滚时提供工具提示或上下文帮助弹出窗口的管理端输入字段(例如自定义元框输入字段)旁边的图标?我想我曾经看到过类似的东西,但它很可能是特定于插件的。有什么想法吗?