Adding a form at the end of the content

I’m trying to add a little form after each post for a plugin I have to code, but I’m not very familiar with WordPress. By using add_action with the_content as hook, I was able to add some text to the content.

My trouble is that the whole text stored in the $content variable seemed to be processed somehow. Specifically, there is a br/ added after each of my input fields, which break the layout of my form. I noticed that I don’t have this issue if I had my form right before the comment form, for instance. But I don’t want to put my form here, I need it to be really just after the post itself, prior to anything related to comments.

Read More

Is there a way to disable this text and html formatting, but only for the form I’ll be adding to the content ? In case there is no easy way to do so, I’ve tried finding a hook that would allow me to add something AFTER the content and BEFORE the comments to do so, but had no luck finding one. Is there such a hook ?

Related posts

Leave a Reply

2 comments

  1. If I understand what you are doing, and the problem you are having, the first thing I’d try is to add your the_content filter with a large priority number so that it runs late, and hopefully after the other formatting filters.

    add_filter('the_content','your_callback',100);

    If that doesn’t do it you may have to remove and reorder some some filters.

    If this is a page and you can edit the template for that page, you could also run get_the_content, apply the content filters to it, then append your form before you echo.

    $content = get_the_content();
    $content = apply_filters('the_content',$content);
    $content = $content.$your_form;
    echo $content;
    
  2. You’ll actually want to filter the content, not use an action. You could wrap your form in the output buffer and append it onto to it, like so:

    function my_content_filter($content) {
        ob_start(); 
    
        // your form here
    
        $form = ob_get_clean();
    
        if (get_post_type() == 'post')
            return $content . $form;
    
        return $content;
    }
    add_filter('the_content', 'my_content_filter');
    

    Let me know if that helps.