XSS prevention helps keep your website safe by stopping bad code from running in users' browsers.
XSS prevention in Wordpress
Introduction
Syntax
Wordpress
<?php // Escape output to prevent XSS echo esc_html( $user_input ); ?>
Use esc_html() to safely show text in HTML.
WordPress has many escaping functions for different contexts.
Examples
Wordpress
<?php echo esc_html( $comment_text ); ?>
Wordpress
<?php echo esc_attr( $input_value ); ?>
Wordpress
<?php echo wp_kses_post( $post_content ); ?>
Sample Program
This example shows how a script tag in user input is turned into safe text so it does not run as code.
Wordpress
<?php // Simulate user input with a script tag $user_input = '<script>alert("XSS")</script>Hello!'; // Safe output using esc_html $safe_output = esc_html( $user_input ); echo "User says: " . $safe_output; ?>
Important Notes
Always escape data when outputting it, never trust user input.
Use the right escaping function for the place where data appears (HTML, attribute, URL, JavaScript).
WordPress provides many helpers like esc_html(), esc_attr(), and wp_kses_post().
Summary
XSS prevention stops harmful scripts from running on your site.
Escape all user data before showing it on pages.
Use WordPress built-in escaping functions for safety.
Practice
1. What is the main purpose of XSS prevention in WordPress?
easy
Solution
Step 1: Understand what XSS means
XSS stands for Cross-Site Scripting, which allows attackers to run harmful scripts on your site.Step 2: Identify the goal of prevention
The goal is to stop these harmful scripts from running to protect users and the site.Final Answer:
To stop harmful scripts from running on your website -> Option CQuick Check:
XSS prevention = stop harmful scripts [OK]
Hint: XSS prevention blocks bad scripts from running [OK]
Common Mistakes:
- Thinking XSS prevention improves speed
- Confusing XSS with SEO improvements
- Believing it adds content automatically
2. Which WordPress function is used to safely escape text before outputting it to prevent XSS?
easy
Solution
Step 1: Identify escaping functions for output
esc_html() escapes text for safe HTML output, preventing scripts from running.Step 2: Differentiate from other functions
wp_kses_post() allows some HTML, sanitize_text_field() cleans input, wp_enqueue_script() loads scripts.Final Answer:
esc_html() -> Option AQuick Check:
Escape text output = esc_html() [OK]
Hint: Use esc_html() to escape text output safely [OK]
Common Mistakes:
- Using sanitize_text_field() for output escaping
- Confusing wp_kses_post() with esc_html()
- Thinking wp_enqueue_script() escapes text
3. What will be the output of this code snippet if user input is
<script>alert('XSS')</script>?
<?php echo esc_html($_GET['input']); ?>
medium
Solution
Step 1: Understand esc_html() effect
esc_html() converts special HTML characters to safe text, so tags show as text, not code.Step 2: Predict output with given input
The script tags will be shown as <script>alert('XSS')</script> literally, not executed.Final Answer:
<script>alert('XSS')</script> (shown as text) -> Option DQuick Check:
esc_html() escapes tags to text [OK]
Hint: esc_html() shows tags as text, not code [OK]
Common Mistakes:
- Thinking script runs despite escaping
- Expecting empty output
- Assuming PHP error occurs
4. Identify the error in this code that tries to prevent XSS:
<?php $user_input = $_POST['comment']; echo $user_input; ?>
medium
Solution
Step 1: Check how user input is output
The code echoes raw user input without escaping, which can run harmful scripts.Step 2: Identify missing security step
It should use an escaping function like esc_html() before echoing to prevent XSS.Final Answer:
Missing escaping function before echoing user input -> Option AQuick Check:
Always escape user output [OK]
Hint: Always escape user data before echoing [OK]
Common Mistakes:
- Thinking $_POST vs $_GET matters here
- Believing variable name causes error
- Assuming raw echo is safe
5. You want to allow some HTML tags like
<strong> and <a> in user comments but prevent scripts. Which WordPress function should you use?hard
Solution
Step 1: Understand the need to allow some HTML safely
esc_html() escapes all HTML, sanitize_text_field() removes tags, wp_strip_all_tags() removes all tags.Step 2: Identify function that allows specific tags
wp_kses() lets you specify allowed tags and attributes, filtering out harmful scripts but keeping safe HTML.Final Answer:
wp_kses() -> Option BQuick Check:
Allow safe HTML = wp_kses() [OK]
Hint: Use wp_kses() to allow safe HTML tags only [OK]
Common Mistakes:
- Using esc_html() which escapes all tags
- Using sanitize_text_field() which removes tags
- Assuming wp_strip_all_tags() allows some HTML
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
