Support

Account

Home Forums General Issues Removing Paragraph Tags from WYSIWYG Fields?

Solved

Removing Paragraph Tags from WYSIWYG Fields?

  • By default, ACF is adding p tags before and after any field I call that’s using the WYSIWYG Editor field type in the ACF backend.

    How can I remove this automatic insertion of paragraph tags? The paragraphs are causing these fields to be bumped down on to their own lines which I don’t want.

    For example: <p><b>Text:</b> <?php the_field(‘name’); ?></p> is being outputted as <p><b>Text:</b> <p><?php the_field(‘name’); ?></p></p> with the extra paragraphs that’s messing up my alignments.

  • Hi @Nitruc

    This is a normal WP behaviour – to turn all new lines into paragraph tags.

    To remove this, you can try loading the value without any formatting.
    You can do this by providing a ‘false’ for the format parameter in the get_field / the_field funcions like so:

    
    the_field('wysiwyg_field', false, false);
    

    Hope that helps.

    Thanks
    E

  • I did

    <div>Name(s): <?php the_field('name', false, false); ?></div>

    But it still outputs like this:

    <div>
    Name(s):
    <p>
    <a href="#">Name</a>
    </p>
    </div>
  • Hi @Nitruc

    Thanks for the follow up. Perhaps the solution is to remove the ‘wpautop’ filter from the formating of the WYSIWYG value.

    Normally, this would be done via:
    remove_filter ('the_content', 'wpautop');

    But ACF uses a custom filter called ‘acf_the_content’, so try this in your functions.php file:

    
    remove_filter ('acf_the_content', 'wpautop');
    

    Hope that helps.

    Thanks
    E

  • Thanks elliot! That did the trick. 🙂

  • Actually, this causes a conflicting problem because I originally applied it to one custom field section, which worked, but now other different ACF WYSIWYG field sections that need line breaks are having their breaks removed. 🙁

  • Hi @Nitruc

    To solve this, you could create a function to remove, echo, and then add back the filter like so:

    
    <?php 
    
    function the_field_without_wpautop( $field_name ) {
    	
    	remove_filter('acf_the_content', 'wpautop');
    	
    	the_field( $field_name );
    	
    	add_filter('acf_the_content', 'wpautop');
    	
    }
    
     ?>
    

    Now you can use this function and afterwards, there will be no issues!

    Thanks
    E

  • Thanks too Elliot!!! I was trying and searching the forums across the net for a solution for my hassle about a custom excerpt function for a acf wysiwg field for weeks. The combination of add_filter(‘acf_the_content’, ‘wpautop’); and
    the_field(‘wysiwyg_field’, false, false); saved me. Drove me crazy 😀

  • Hi,

    Does
    the_field('wysiwyg_field', false, false);
    work for sub fields as well? I am trying to use this within a flexible content loop, but with no luck so far. Thanks!

  • I, too, am trying to use this same method but with a get_sub_field call. I have modified Elliot’s original code but I’m unable to get the field without the wpautop happening. Any suggestions on this one?

    Here’s what I am attempting to use:

    function get_sub_field_without_wpautop($field_name) {
    
    	// Temporarily remove the filter for this get_sub_field call
    	remove_filter('acf_the_content', 'wpautop');
    
    	// Get the sub field without the wpautop filter
    	$field = get_sub_field($field_name);
    
    	// Re-establish the wpautop filter for everything
    	add_filter('acf_the_content', 'wpautop');
    
    	// Return our non-wpautop'd field
    	return $field;
    }
    
  • Does this work with the new ACF Pro?

    I tried this, to no effect:

    function the_field_without_wpautop( $field_name ) {
    	
    	remove_filter('acf_the_content', 'wpautop');
    	
    	the_field( $field_name );
    	
    	add_filter('acf_the_content', 'wpautop');
    	
    }
  • Looking at a solution to this with ACFPRO as well.

    Would like to choose which WYSIWYG editors to remove P tags from.

    Thanks

  • Had the same issue. I used Strip Tags

    strip_tags( get_sub_field( 'field_name' ) );

  • Actually, WordPress has a function wp_strip_all_tags which would work as well. I wrapped it in a function below to work with sub fields.

    function jw_strip_all_tags_from_sub_field( $field )
    {
    
    $field 	        = get_sub_field( $field );
    $field_stripped = wp_strip_all_tags(	$field );
    
    return $field_stripped;
    
    }
    
  • I tried the function like Elliot showed above and it worked great!
    I did one per filter for the moment in my header.php:

    function the_content_without_filters( $the_content=null ) {
        remove_filter('the_content', 'wpautop');
        if( $the_content ) {
          the_content( $the_content );
        } else {
          the_content();
        }
        add_filter('the_content', 'wpautop');
      }
    
      function the_excerpt_without_filters( $the_excerpt=null ) {
        remove_filter('the_excerpt', 'wpautop');
        if( $the_excerpt ) {
          the_excerpt( $the_excerpt ) ;
        } else {
          the_excerpt();
        }
        add_filter('the_excerpt', 'wpautop');
      }
    
      function the_field_without_filters( $the_field=null ) {
        remove_filter('acf_the_content', 'wpautop');
        if( $the_field ) {
          the_field( $the_field );
        } else {
          the_field();
        }
        add_filter('acf_the_content', 'wpautop');
      }

    I guess you can also inverse if you need to:

    
    function the_field_without_filters( $the_field=null ) {
        add_filter('acf_the_content', 'wpautop');
        if( $the_field ) {
          the_field( $the_field );
        } else {
          the_field();
        }
        remove_filter('acf_the_content', 'wpautop');
    }
  • we just upgraded our website with latest ACF plugin update Version 5.3.6.1.
    Our WP 4.4.2

    Our font-size just changed on few pages due to ACF adding <p></p>
    we have below code in the functions.php so not sure why is not working now after update.

    if( function_exists('acf_add_options_page') )
    {
    	acf_add_options_page();
    	acf_add_options_sub_page(' Header ');
    	acf_add_options_sub_page(' Footer ');
    	acf_add_options_sub_page(' Shortcodes ');
    	acf_add_options_sub_page(' Contact-PageContent ');
    	
    	// remove <p></p> from acf editor
    	remove_filter ('acf_the_content', 'wpautop');
    }
  • Any one has any thoughts on this?

  • Hi Elliot

    It doesn’t work (ACF 5.3.9.2, WP 4.5.3). Anything has changed?

  • I could do with an answer regarding this also

  • This question has been marked as solved, but apparently it is not.

    ACF Pro latest in latest WP. None of the filters above seem to work.

  • Hi,

    Not sure if this will work for you, but it worked for me..

    //Remove WPAUTOP from ACF TinyMCE Editor
    function acf_wysiwyg_remove_wpautop() {
        remove_filter('acf_the_content', 'wpautop' );
    }
    add_action('acf/init', 'acf_wysiwyg_remove_wpautop');

    This will remove it from all ACF WYSIWYG fields, but you should be able to target this to just one field..

    The difference between this function and the others I saw in this thread, is that the action is added on ‘acf/init’

  • None of this worked for me…
    I tried this:

    add_filter('tiny_mce_before_init', function($init) {
        $init['wpautop'] = false;
        $init['forced_root_blocks'] = false;
        $init['force_p_newlines'] = false;
        $init['force_br_newlines'] = true;
        return $init;
    });

    and

    add_action('acf/init', function() {
        
        remove_filter('acf_the_content', 'wpautop' );
        remove_filter('the_content', 'wpautop' );
    });

    none of this works.
    Sugestions?

  • Try adding a priority to the function, this worked for me:

    function acf_wysiwyg_remove_wpautop() {
        remove_filter('acf_the_content', 'wpautop' );
    }
    add_action('acf/init', 'acf_wysiwyg_remove_wpautop', 15);
  • Still not working… I tried with priorities from 1 to 100, with no success.
    My version of ACF is 5.6.1
    Thanks

  • Your code is different to what I posted.. What I posted above with a priority of 15 worked for me. Unfortunately I don’t have any other suggestions.

Viewing 25 posts - 1 through 25 (of 34 total)

The topic ‘Removing Paragraph Tags from WYSIWYG Fields?’ is closed to new replies.