0

I want to add a table after the_content with a hook, but it's adding <p> tags in certain areas. This is a plugin, I don't want to remove <p> tags from all the_content, just the table I'm trying to add.

Here's an example, but my table is bigger and inside it's adding p tags in different spots.

function addTable( $content ) {
  $table = '<table><tbody><tr><td>Example html here</td></tr></tbody></table>';

  $content .= $table;

  return $content;
}

add_filter( 'the_content', 'addTable', 20 );

Someone asked this in 2012, no answer but I'm trying to do the same thing: Adding a form at the end of the content

I do not want to remove <p> tags from a theme's entire blog post, only the content I am appending. In this case, I'm appending a table.

Is there a way to disable the formatting for only the content I'm appending? Otherwise, how is it possible in WordPress to attach a form or table to a post with hooks without it adding formatting?

3
  • unrelated to your problem, but there is a mistake in the add_filter call, addTable should be "addTable"
    – Tom J Nowell
    Commented Feb 19, 2022 at 1:16
  • wpautop is added with a priority of 10, but you're adding your table with a priority of 20 which happens afterwards. Are you finding the p tags via the dev tools? Or have you inspected the raw view-source version and found them there too? Is your example able to reproduce the issue as is? Or would modifications be needed to reproduce the issue, aka changing the Example html here part?
    – Tom J Nowell
    Commented Feb 19, 2022 at 1:21
  • @TomJNowell Thanks I updated my question. I think I tried 10 already, but I'll try different numbers, not sure if that's it. Commented Feb 19, 2022 at 21:21

1 Answer 1

0

Pull from the provided answer back in 2012 and testing with the 2021 theme it works as intended.

function addTable( $content ) {
        
        ob_start(); 
        echo '<table><tbody><tr><td>Example html here</td></tr></tbody></table>';
        $table = ob_get_clean();
        
        $content .= $table;
      
        return $content;
    }
      
    add_filter( 'the_content', "addTable", 20 );

enter image description here

2
  • this is functionally equivalent to what the OP posted, output buffers do nothing to help here other than making the creation of $table more complicated. I agree that addTable needed to be wrapped in quotes though
    – Tom J Nowell
    Commented Feb 19, 2022 at 1:16
  • Correct, I just used one of the examples from the included link that was stated to work. I could not get it to add p tags regardless of how large I made the table.
    – Bazdin
    Commented Feb 19, 2022 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.