尝试使用wpcf7 add_meta_boxes钩子添加元框

时间:2014-04-14 14:06:46

标签: wordpress

我已经编写了一个扩展Contact Form 7插件的插件。我想在编辑区域为每个表单添加一个元数据框,显示一个启用/禁用插件扩展名的复选框。

/**
 * Add edit options to each CF7 form
 */

add_action('wpcf7_add_meta_boxes', 'wpcf7ev_add_form_options');

function wpcf7ev_add_form_options() {

    add_meta_box('wpcf7ev_form_options', 'Email Verification', 'wpcf7ev_display_options', 'wpcf7_contact_form');

}

function wpcf7ev_display_options() {

    wp_mail('myemail@gmail.com', 'debug', 'adding checkbox');

    $wpcf7ev_options['active'] = 1;
?>

<input type="checkbox" id="wpcf7ev-active" name="wpcf7ev-[active]" value="1"<?php echo ( $wpcf7ev_options['active']==1 ) ? ' checked="checked"' : ''; ?> />
<label for="wpcf7ev-active">Use email verification</label>
<?php

}

wpcf7ev_display_options()函数似乎无法被调用。我检查了帖子类型,其中一个是&#34; wpcf7_contact_form&#34;。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我知道这个问题是永远问的,但是我为所有希望将自定义元信息添加到联系表7的人找到了一个解决方案:

/*
 * Adds an additional panel to the Contact Form 7 contact form edit screen with an input that will save to the post meta of the contact form.
 *
 */

/*
 * Sets up the fields inside our new custom panel
 * @param WPCF7_ContactForm $post - modified post type object from Contact Form 7 containing information about the current contact form
 */
function wpcf7_custom_fields ($post) {
    ?>
    <h2><?php echo esc_html( __( 'Custom Fields', 'contact-form-7' ) ); ?></h2>
    <fieldset>
        <legend>Fields for other information we want to save to a contact form not included in the base plugin</legend>
        <label for="wpcf7-custom-field">Custom Field</label>
        <input type="text" id="wpcf7-custom-field" name="wpcf7-custom-field" value="<?php
        //$post here is not a traditional WP Post object, but a WPCF7_ContactForm object, $post->id is private, so we need to use the id() function to get the post ID

        echo get_post_meta($post->id(), 'custom_field', true) ?>"
        />
    </fieldset>
    <?php
}

/*
 * Adds our new Custom Fields panel to the Contact Form 7 edit screen
 *
 * @param array $panels - an array of all the panels currently displayed on the Contact Form 7 edit screen
 */

function add_cf7_panel ($panels) {
    $panels['custom-fields'] = array(
        'title' => 'Custom Fields',
        'callback' => 'wpcf7_custom_fields',
    );

    return $panels;
}

add_filter('wpcf7_editor_panels', 'add_cf7_panel');

/*
 * Hooks into the save_post method and adds our new post meta to the contact form if the POST request contains the custom field we set up earlier
 * @param $post_id - post ID of the current post being saved
 */

function save_wpcf7_custom_fields($post_id) {
    if (array_key_exists('wpcf7-custom-field', $_POST)) {
        update_post_meta(
            $post_id,
            'custom_field',
            $_POST['wpcf7-custom-field']
        );
    }
}

add_action('save_post', 'save_wpcf7_custom_fields');

这是为wpcf7_editor_panels添加过滤器(根据Digvijayad的建议),该过滤器将新面板添加到编辑屏幕。该面板包含一个输入,该输入是用于保存联系表的表单的一部分。当我们保存此表单时,联系表单7会调用save_post,并且新输入及其值现在已成为请求的一部分,因此我们可以使用它们来更新所涉及的实际post对象上的post meta。希望这可以帮助某人。