How to Override Contact Form 7 Output Without Editing Plugin Files

override Contact Form 7 output

Do you want to override Contact Form 7 output without touching its core plugin files? Many developers face this challenge when they need to change success messages, validate user input, or save form submissions. In this post, you’ll learn how to override Contact Form 7 output using WordPress hooks safely and effectively.

As WordPress developers, we often need to customize plugins to match our clients’ needs, but without editing their core files. One common task is to override Contact Form 7 output dynamically. For example, we may want to display different success messages for logged-in and logged-out users or save form submissions into a custom post type.

In this post, you’ll learn:

  • How to create settings to control success messages for different users
  • How to override the default Contact Form 7 message
  • How to save submitted data into a custom post type

If you haven’t installed Contact Form 7 yet, you can easily find it in the WordPress plugin directory. Just go to your WordPress dashboard, navigate to Plugins → Add New, and search for “Contact Form 7.” Alternatively, you can download Contact Form 7 directly from the WordPress.org plugin repository. For reliable hosting during development or staging, you might also consider using Flywheel — a popular WordPress hosting platform tailored for developers and agencies.


Why Override Contact Form 7 Output?

Contact Form 7 lets you edit success messages from its “Messages” tab. But in some real-world scenarios, developers need more control. For example:

  • Show a personalized message for logged-in users
  • Encourage guest users to explore more pages
  • Save submitted form data to a custom post type (like a pet gallery)

Step 1: Add Settings for Success Messages

We first create a sub-menu page under Settings where an admin can define different messages.

add_submenu_page(
   'options-general.php',
   'Contact Form Intregation',
   'Contact Form Intregation',
   'manage_options',
   'contact-form-intregation',
   'display_options_for_contact_form'
);

function display_options_for_contact_form() {
   $logged_in_msg = get_option('logged_in_msg', 'Thank you! We have received your request and will get back to you shortly.');
   $logged_out_msg = get_option('logged_out_msg', 'Thank you! Please checkout our pages');
   ?>
   <div class="container">
       <h2><?php _e('Contact Form Integration', 'text-domain'); ?></h2>
   </div>
   <form method="post">
       <div class="custom_msg">
           <div class="logged_in">
               <label for='looged_in_message'>Logged in User:
                   <input type="text" name='logged_in_msg' value="<?php echo esc_html($logged_in_msg); ?>" />
               </label>
           </div>
           <div class="logged_out">
               <label for='looged_out_message'>Logged out User:
                   <input type="text" name="logged_out_msg" value="<?php echo esc_html($logged_out_msg); ?>" />
               </label>
           </div>
           <div class="submit_btn">
               <input type="submit" name="submit" value="Submit" />
           </div>
       </div>
   </form>
<?php }

if (isset($_POST['submit'])) {
   update_option('logged_in_msg', $_POST['logged_in_msg']);
   update_option('logged_out_msg', $_POST['logged_out_msg']);
}

Step 2: Override Contact Form 7 Output Dynamically

Now we’ll modify the success message based on the user’s login status using the wpcf7_ajax_json_echo filter.

add_filter('wpcf7_ajax_json_echo', 'custom_success_message_cf7', 10, 1);

function custom_success_message_cf7($response) {
   if ($response['status'] == 'mail_sent') {
       if (is_user_logged_in()) {
           $response['message'] = get_option('logged_in_msg');
       } else {
           $response['message'] = get_option('logged_out_msg');
       }
   }
   return $response;
}

Bonus Tip: You can also change messages from the Contact → Messages tab, but this method gives developers more flexibility to show messages dynamically.

Step 3: Save Contact Form Data to Custom Post Type

Let’s say you’re building a Pet Gallery where users can submit their pet details. You can create a custom post type and save the form data directly into it.

Register the Custom Post Type:

function create_pet_gallery_cpt() {
    register_post_type('pet_gallery', array(
        'labels' => array(
            'name' => 'Pet Gallery',
            'singular_name' => 'Pet',
        ),
        'public' => true,
        'has_archive' => false,
        'publicly_queryable' => false,
        'exclude_from_search' => true,
        'menu_icon' => 'dashicons-camera',
        'supports' => array('title', 'editor', 'thumbnail'),
        'rewrite' => false,
    ));
}
add_action('init', 'create_pet_gallery_cpt');

Add Custom Fields with a Meta Box

function breed_meta_box_setup() {
    add_meta_box('breed_meta_box', 'Enter Pet Details', 'breed_meta_box_callback', 'pet_gallery');
}
add_action('add_meta_boxes', 'breed_meta_box_setup');

function breed_meta_box_callback($post) {
    $custom_value = get_post_meta($post->ID, 'pet_breed', true);
    $featured_pet = get_post_meta($post->ID, '_featured_pet', true);
    wp_nonce_field('breed_meta_box_nonce_action', 'breed_meta_box_nonce');
    ?>
    <input type="text" name="pet_breed" value="<?php echo esc_attr($custom_value); ?>" />
    <label><input type="checkbox" name="featured_pet" value="1" <?php checked($featured_pet, '1'); ?> /> Winner Pet</label>
<?php }

function save_breed_meta_box_data($post_id) {
    if (!isset($_POST['breed_meta_box_nonce']) || !wp_verify_nonce($_POST['breed_meta_box_nonce'], 'breed_meta_box_nonce_action')) return;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    if (!current_user_can('edit_post', $post_id)) return;

    update_post_meta($post_id, 'pet_breed', sanitize_text_field($_POST['pet_breed']));
    update_post_meta($post_id, '_featured_pet', isset($_POST['featured_pet']) ? '1' : '0');
}
add_action('save_post', 'save_breed_meta_box_data');

Step 4: Save Form Submissions to the CPT

Hook into wpcf7_mail_sent to save submitted data:

function save_pet_submission_cf7($cfdata) {
    $form_id = 10; // Change this to your actual CF7 form ID
    if ($cfdata->id() != $form_id) return;

    $submission = WPCF7_Submission::get_instance();
    if (!$submission) return;

    $data = $submission->get_posted_data();
    $pet_name = sanitize_text_field($data['pet_name']);
    $pet_breed = sanitize_text_field($data['pet_breed']);
    $pet_fact = sanitize_textarea_field($data['pet_fact']);
    $current_user = wp_get_current_user();

    $post_id = wp_insert_post(array(
        'post_title' => $pet_name,
        'post_content' => $pet_fact,
        'post_status' => 'publish',
        'post_type' => 'pet_gallery',
        'post_author' => $current_user->ID,
    ));

    update_post_meta($post_id, 'pet_breed', $pet_breed);

    // Handle featured image
    $uploaded_files = $submission->uploaded_files();
    if (isset($uploaded_files['pet_image'])) {
        $pet_image = reset($uploaded_files['pet_image']);
        $upload_dir = wp_upload_dir();
        $filename = basename($pet_image);
        $new_file = $upload_dir['path'] . '/' . $filename;

        if (copy($pet_image, $new_file)) {
            $wp_filetype = wp_check_filetype($filename, null);
            $attachment = array(
                'guid' => $upload_dir['url'] . '/' . $filename,
                'post_mime_type' => $wp_filetype['type'],
                'post_title' => sanitize_file_name($filename),
                'post_status' => 'inherit',
                'post_author' => $current_user->ID,
            );

            $attach_id = wp_insert_attachment($attachment, $new_file, $post_id);
            require_once ABSPATH . 'wp-admin/includes/image.php';
            $attach_data = wp_generate_attachment_metadata($attach_id, $new_file);
            wp_update_attachment_metadata($attach_id, $attach_data);
            set_post_thumbnail($post_id, $attach_id);
        }
    }
}
add_action('wpcf7_mail_sent', 'save_pet_submission_cf7');

Recent Posts

>>How to Create a WordPress Widget to Filter Posts by Category Using AJAX

Conclusion

Contact Form 7 is powerful, but by default, it doesn’t support dynamic messages or saving data to a custom post type. As a developer, knowing how to override Contact Form 7 output using hooks gives you full control — without ever editing the plugin’s core.

By combining smart settings and WordPress actions, you can customize form behavior exactly as your project demands.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.