最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Add hook after content without formatting

programmeradmin6浏览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?

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?

Share Improve this question edited Feb 19, 2022 at 21:19 Justin Breen asked Feb 18, 2022 at 21:16 Justin BreenJustin Breen 134 bronze badges 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. – Justin Breen Commented Feb 19, 2022 at 21:21
Add a comment  | 

1 Answer 1

Reset to default 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 );

发布评论

评论列表(0)

  1. 暂无评论