Disable formatting on textarea

I’m using the following code in my plugin to display a <textarea> field on a page:

echo '<textarea required="required" name="Message" class="textarea" cols="70" rows="10">'.esc_textarea($message).'</textarea>';

However, WordPress seems to be inserting <p> and <br /> tags into the textarea contents if the message contains newline characters. I’m assuming this is being caused by the default wpautop() filter.

Read More

Is there a way to disable filtering of <textarea> contents on a page?

Edit

Some more information about how my plugin works. It creates a hook for the_content, which will append data to the contents of certain pages. I still want wpautop formatting to be applied to the appended content, just not for the <textarea> field.

Here is a rough outline of how my plugin is setup:

function get_special_content()
{
    $text = '';

    // Customized content is appended to text here
    // including the textarea field

    return $text;
}

function my_plugin_content_hook($content)
{
    if(is_some_special_page()) {
        return $content.get_special_content();
    }

    return $content;
}

add_action('the_content', 'my_plugin_content_hook');

Related posts

Leave a Reply

2 comments

  1. Add a priority to push your function to the end of the hook queue.

    add_action('the_content', 'my_plugin_content_hook', 1000);
    

    Then, in your get_special_content function you will need to apply wpautop manually to the content to which you want it applied.

    function get_special_content()
    {
        $text = '';
        $autopeed = 'content to autop';
        $text .= apply_filters('wpautop',$autopeed);
        $text .= "your textarea code";
        return $text;
    }
    

    I believe that will solve your problem.

  2. I discovered that wpautop will preserve the contents of <pre> tags. By placing a <pre> tag around the <textarea> tag, I am able to prevent the text area contents from being modified.

    echo '<pre><textarea>'.esc_textarea($message).'</textarea></pre>';
    

    I’m not sure why wpautop doesn’t also ignore <textarea> tags by default. I can’t imagine a scenario where you would want wpautop to be applied to the contents of a text area. Maybe it’s just an oversight.