Is there a filter for allowing HTML / Javascript into a custom field textarea? I'm using get_post_meta.
On the frontpage I want the HTML / Javascript to run, not to be displayed.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityIs there a filter for allowing HTML / Javascript into a custom field textarea? I'm using get_post_meta.
On the frontpage I want the HTML / Javascript to run, not to be displayed.
The correct answer is esc_textarea()
.
Example:
<textarea><?php echo esc_textarea($whatever); ?></textarea>
I answered an identical question earlier today HERE which tackled the same issue. In the case where you don't want to use a plugin like that suggested by EarnestoDev, you can simply wrap the output of your <textarea></textarea>
with PHP's htmlspecialchars
function like so;
<textarea name="my_html">
<?php echo htmlspecialchars($content); ?>
</textarea>
Regardless of whether its a textarea
or an input
field etc, you still need to wrap your input in htmlspecialchars
to preserve your tags from being stripped.
esc_textarea()
function is what you want for this case. If he wanted to put something in an attribute (like the value field of an input tag), then he would use esc_attr()
instead.
esc_textarea
is the API call you want to be making and what I should have said in context was htmlspecialchars
which is what underpins esc_textarea
. So to the OP, use esc_textarea
instead, although using htmlspecialchars
is perfectly valid PHP, its not the official API call, regardless of whether it'll do you no harm, at the very least you gain an insight to what esc_textarea
is using, should you venture outside of the WP platform. htmlentities
is also valid but its use-case dependent and probably not in context. @Otto I'll am mend my other answer.
Use the plugin I posted here: Problem with <code> tag .
Does what you need and more!
Yes, there must be. When you save this into a custom field with a key let's say "test"...
<p>Test1</p><p>Test2</p>
You will get Test1Test2 when you run...
echo get_post_meta($post->ID, 'test', true);
If I knew which filter was responsible for this I'd just do a remove_filter(). It would be great if there was a plugin that would show you what filters are registered to a specific hook. (There is a get_post_meta hook defined in /wp-includes/meta.php)