2

I'm sending the content of a custom post type in a plain text email (it's to send competition entries to a panel of judges), so I need to make sure that $post->post_content is correctly sanitized first.

Is there a filter I can use for this, or if not, what sanitization do I need to do?

Update: I've just found wp_strip_all_tags in wp-includes/formatting.php, is this what I need?

2

You will want to use sanitize_email(); as follows:

return( sanitize_email( $email ) );

Here is the Codex link so you have it: http://codex.wordpress.org/Function_Reference/sanitize_email

Cheers!

  • that's great thanks, exactly what i was looking for – Simon Blackbourn Apr 7 '12 at 12:57
  • Yeah, in the Professional WordPress Plugin Development by Wrox it explains in detail why it's best to use the sanitize_email function. There's also a validation function is_email which is really cool too. Cheers! – David Apr 7 '12 at 17:49
  • But the questioner wants to sanitize the message body and this function is for sanitising an email address. – Andy Macaulay-Brook Jun 23 '18 at 16:19
0

I'm not sure why the accepted answer here was accepted since it is not actually going to work.

The OP was how to sanitize the email content. sanitize_email() sanitizes an email address. Sure, it won't throw an error, but it doesn't actually do anything.

To sanitize the content, it depends on what is actually intended to be in the content to determine what would be appropriate.

One generic possibility would be sanitize_textarea_field. This is for the HTML text area field, but it will maintain line breaks. It will strip out all tags.

However, if the email is intended to be HTML formatted, then you don't want to strip all tags. In that case, you'd want to use something that allows the tags you want, but strips out the tags you don't. For that, use wp_kses().

To use wp_kses() to sanitize your HTML email content, pass the content and an array including allowed tags to the function:

$allowed_tags = array(
  'p' => array(
    'id' => array(),
    'class' => array(),
  ),
  'a' => array(
    'href' => array(),
    'title' => array()
  ),
  'br' => array(),
  'em' => array(),
  'strong' => array(),
);

$sanitized_content = wp_kses( $post->post_content, $allowed_tags );

A simplified variation on that above method would be to use wp_kses_post(). This function has preset the allowed tags, which makes it easier since you don't have to define what tags and attributes are allowed. It's primarily whatever is allowed for post content.

$sanitized_content = wp_kses_post( $post->post_content );

If it's regular post content, then it probably already went through this. If it's a custom post type, then it depends.

So which should you use? As I mentioned in the beginning, it depends on the content and how it will be used. If it's plain text, use something that strips all tags. If it's HTML, use wp_kses() or a variant.

See related information on sanitizing in the WP Codex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.