How to Remove and Tags from Contact Form 7 in WordPress

WPTurbo » Snippets » How to Remove and Tags from Contact Form 7 in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you wrestling with unwanted paragraph or line break tags appearing in your Contact Form 7 forms on WordPress? Not to worry, there is indeed a simple solution to get rid of these unnecessary tags which can sometimes erupt our neatly arranged forms. In this article, we will guide you step-by-step on how to remove the pesky "p" and "br" tags from your Contact Form 7 on WordPress.

					function wpturbo_remove_pb_cf7($form) {
    $form = str_replace(array('<p>','</p>','<br />'), '', $form);
    return $form;
}

add_filter('wpcf7_autop_or_not', '__return_false');
add_filter('wpcf7_form_elements', 'wpturbo_remove_pb_cf7');
				

The code snippet involves two parts: the creation of a function named wpturbo_remove_pb_cf7(), and the addition of hooks into wpcf7_autop_or_not and wpcf7_form_elements filters.

The function wpturbo_remove_pb_cf7() is used to remove all <p> and <br /> tags from the Contact form 7 output.

The $form parameter that is passed to our function is a string containing the HTML markup of the form. This string is filtered through the str_replace() function, which searches for specific items in the string and replaces them with something else. In this case, we’re replacing <p>, </p>, and <br /> tags with an empty string '', effectively removing these HTML tags from the form’s output.

The modified $form string is then returned by the function, ensuring that the modifications are preserved in the form output.

Next, we have two filter hooks:

1.add_filter('wpcf7_autop_or_not', '__return_false');

This filter controls whether Contact Form 7 does automatic paragraph and line break formatting or not for its forms. By default, this is set to true, which means Contact Form 7 will wrap form elements in paragraph tags <p> and insert break tags <br /> where it deems it applicable. However, by returning false with the __return_false function, this disables the automatic wrapping of elements in paragraph tags and automatic line breaks in Contact Form 7 forms.

2.add_filter('wpcf7_form_elements', 'wpturbo_remove_pb_cf7');

This filter hook applies the function wpturbo_remove_pb_cf7() to the form elements before they are output to the screen. It takes the form elements as an argument, processes them as per the instructions in wpturbo_remove_pb_cf7(), and then the modified form elements are returned back. Hence, whenever the Contact Form 7 form elements are generated, our custom function is used to remove the paragraph and break line tags.

So overall, by using this code snippet, we are preventing the Contact Form 7 from automatically inserting <p> and <br/> tags into the form code and also manually removing them if they are already in the form. This helps in controlling the design and layout of the form according to the developer’s preference.

Register an account to save your snippets or go Pro to get more features.