Trying to add a meta box using wpcf7_add_meta_boxes hook

I’ve written a plugin to extend the Contact Form 7 plugin. I want to add a metabox in the editing area for each form that shows a checkbox that enables/disables the plugin extension.

/**
 * 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

}

The wpcf7ev_display_options() function doesn’t seem to ever be called though. I checked the post types and one of them is “wpcf7_contact_form”.

Read More

What am I missing?

Related posts

Leave a Reply

1 comment

  1. I know this question was asked forever ago, but I found a solution for anyone who’s looking to add custom meta information to Contact Form 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');
    

    What this does is add a filter to wpcf7_editor_panels (per Digvijayad’s suggestion) that adds a new panel to the edit screen. That panel contains an input which is part of the form for saving the contact form. Contact Form 7 calls save_post when we save this form, and our new input and its value are now part of the request, so we can use them to update the post meta on the actual post object in question. Hope this helps someone out.