To remove the automatic <br />
and <p>
tags that Contact Form 7 outputs, you can use the wpcf7_autop_or_not()
filter provided by the plugin. This filter allows you to control whether or not automatic paragraph function is applied to the output of Contact Form 7, which is responsible for adding the <br />
and <p>
tags.
To use this filter, you’ll need to do one of the following:
Option 1: Add a filter to functions.php
One thing to note about this method is it will effect all existing Contact Form 7 forms on your website, so if you add this code to your website, you’ll want to check to make sure all forms are displaying properly.
The easiest way is to add a filter to your functions.php file. If your Theme File Editor is disabled (it generally is for improved security) then you’ll need to edit the functions.php file via FTP or some other means which varies by your hosting provider.
- Go to your WordPress admin dashboard https://yoursite.com/wp-admin/
- Click on Appearance > Theme File Editor
- On the right side you’ll see Theme Files, find the Theme Functions (functions.php) file and select it.
- Add the following code snippet to the file content.
- Click Update File in the bottom left of the page.
// Remove automatic <p> and <br /> from Contact Form 7 add_filter( 'wpcf7_autop_or_not', '__return_false' );
Option 2: Only add filter to specific forms
You might have a site that has a bunch of forms on it and you only want to remove the automatic paragraphs from specific forms. You can use the wpcf7_autop_or_not filter hook to control whether or not the wpautop() function is applied to specific form IDs. This can be helpful if you have legacy forms that rely on the automatically applied styling.
To use this hook to control the wpautop()
function for specific form IDs, you would need to add the following code to your WordPress theme’s functions.php file:
// Remove automatic <p> and <br /> from specific Contact Form 7 forms add_filter( 'wpcf7_autop_or_not', function( $use_autop ) { $wpcf7 = WPCF7_ContactForm::get_current(); $form_id = $wpcf7->id; if ( $form_id == 872 ) { $use_autop = false; } return $use_autop; }, 10, 2 );
Option 3: Add definition to wp-config.php
If you are unable to update the functions.php file or you are running a multisite and would like the make this change for all websites on your WordPress install you can add this snippet to the wp-config.php file. In order to do this you’ll need direct access to the server, via FTP or some other means. Be sure to always make backups of these files before changing them.
// Remove automatic <p> and <br /> from Contact Form 7 define('WPCF7_AUTOP', false);