Trouble with textarea in TinyMCE Editor

Came across an issue where the TinyMCE editor inside WordPress was stripping out <textarea> tags as I switched between the HTML and Visual editors, and the page I was working on needed a textarea.

After digging through Google I came across a pretty good reason. A textarea inside another textarea can cause problems so they’re not allowed. Makes sense, but not really a solution.

What I did was add a bit of code to my theme functions.php file to create a pseudo shortcode.

function allow_textareas($content) {
	return preg_replace(
		'/\[\s*textarea(.*?)\](.*)\[\s*\/textarea\]/i',
		'<textarea$1>$2</textarea>',
		$content);
}
add_filter('the_content','allow_textareas');

It’s not really a true short code, but it does work. And I didn’t want to do an actual short code because I would have to worry about the different possible attributes. Regex allowed me to just replace what I needed to and leave the rest.

Now all I have to do is [textarea][/textarea] and the function takes care of replacing it with the right HTML tags.


This entry was posted in WordPress and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2 Responses to Trouble with textarea in TinyMCE Editor

  1. Alma says:

    This worked beautifully for me. I had searched everywhere online to get the textarea to work. Thanks so much for this!