Security is a cornerstone of WordPress development. Whether you’re building a theme, plugin, or custom application, properly sanitizing and escaping data is critical to protecting your site from vulnerabilities like Cross-Site Scripting (XSS) attacks. WordPress security functions help developers handle data safely, including esc_html(), esc_url(), wp_kses(), and sanitize_text_field(). In this guide, we’ll explore these functions in detail, explain their differences, and clarify when to use each one.
Before diving into the specifics, let’s understand why these functions are essential:
Now, let’s break down each function.

esc_html() converts special HTML characters (like <, >, &, “, and ‘) into their corresponding HTML entities. This prevents HTML or JavaScript from being rendered in the browser.
For example, if a user inputs <script>alert(‘Hacked!’)</script>, esc_html() will convert it to:
<script>alert('Hacked!')</script>
This code ensures the text is displayed as plain text rather than executed as code.
$user_input = '<strong>Hello World</strong>';
echo '<div>' . esc_html($user_input) . '</div>';
Output:
<div><strong>Hello World</strong></div>
The browser displays <strong>Hello World</strong> as plain text.
esc_url() sanitizes URLs to ensure they’re safe for use in links, redirects, or embeds. It:
If a user provides javascript:alert(‘Hacked!’), esc_url() will sanitize it to an empty string or a safe value, depending on context.
$user_url = 'javascript:alert("Hacked!");';
echo '<a href="' . esc_url($user_url) . '">Click Me</a>';
Output:
<a href="">Click Me</a>
The malicious JavaScript is stripped, leaving a safe, empty link.
wp_kses() (Kses Strips Evil Scripts) allows you to permit specific HTML tags and attributes while stripping others. It’s useful when you need to allow some HTML but want to block potentially dangerous code.
You define an array of allowed tags and attributes. For example, you might allow <a> tags with href and title attributes but block <script> tags.
$allowed_tags = array(
'a' => array(
'href' => array(),
'title' => array()
),
'strong' => array()
);
$user_input = '<a href="https://example.com" onclick="alert(\'Hacked!\')">Safe Link</a>';
echo wp_kses($user_input, $allowed_tags);
Output:
<a href="https://example.com">Safe Link</a>
The onclick attribute is stripped, leaving a safe link.
sanitize_text_field() sanitizes text input by:
If a user submits <script>alert(‘Hacked!’)</script>, sanitize_text_field() converts it to:
<script>alert('Hacked!')</script>
This code ensures the input is safe to store in the database.
$user_input = '<h1>Hello World!</h1> ';
$sanitized_input = sanitize_text_field($user_input);
// Output: "<h1>Hello World!</h1>"
1 – Using esc_html() on URLs:
Incorrect:
<a href="<?php echo esc_html($url); ?>">Link</a>
Correct:
<a href="<?php echo esc_url($url); ?>">Link</a>
2 – Overusing wp_kses():
Avoid allowing unnecessary tags. For example, permitting <script> in comments could introduce vulnerabilities.
3 – Skipping Sanitization Before Saving:
Always use sanitize_text_field() (or similar functions) before storing user input in the database.
$input = sanitize_text_field($_POST['text']);
echo esc_html($input);
Understanding the differences between esc_html(), esc_url(), wp_kses(), and sanitize_text_field() is crucial for building secure WordPress applications. By following the principles of sanitizing input and escaping output, you can protect your site from vulnerabilities while maintaining flexibility. Always choose the right function for the context, and when in doubt, err on the side of caution by stripping unnecessary code. For further reading, explore the WordPress Codex and OWASP XSS Prevention Cheat Sheet.

Hassan Tahir wrote this article, drawing on his experience to clarify WordPress concepts and enhance developer understanding. Through his work, he aims to help both beginners and professionals refine their skills and tackle WordPress projects with greater confidence.