📖 Scenario: You are building a simple WordPress theme that displays user-submitted comments safely.Users can enter comments on posts, but you want to prevent harmful scripts from running on your site.
🎯 Goal: Build a WordPress theme snippet that safely outputs user comments by preventing cross-site scripting (XSS) attacks using WordPress functions.
📋 What You'll Learn
Create a PHP array called $comments with exact user comment strings including one with a script tag
Add a variable $allowed_tags that defines which HTML tags are allowed in comments
Use wp_kses() function to sanitize each comment allowing only the allowed tags
Echo the sanitized comments inside <div> elements in a loop
💡 Why This Matters
🌍 Real World
Preventing XSS attacks is critical when displaying user-generated content on WordPress sites to keep visitors safe.
💼 Career
Web developers must know how to sanitize and escape content properly to build secure WordPress themes and plugins.
Progress0 / 4 steps
1
Create the comments data array
Create a PHP array called $comments with these exact strings: 'Hello, this is safe!', 'Nice post! <script>alert("XSS")</script>', and 'Thanks for sharing.'
Wordpress
Hint
Use square brackets [] to create the array and include the exact strings with quotes.
2
Define allowed HTML tags for comments
Add a variable called $allowed_tags that allows only <b> and <i> tags in comments. Use an associative array with keys 'b' and 'i' and empty arrays as values.
Wordpress
Hint
Allowed tags array keys are tag names as strings, values are empty arrays.
3
Sanitize comments using wp_kses
Use a foreach loop with variable $comment to iterate over $comments. Inside the loop, create a variable $safe_comment that stores the sanitized comment using wp_kses($comment, $allowed_tags).
Wordpress
Hint
Use foreach ($comments as $comment) and assign sanitized result to $safe_comment.
4
Output sanitized comments inside div elements
Inside the foreach loop, echo each $safe_comment wrapped in a <div> element. Use echo '<div>' . $safe_comment . '</div>'; exactly.
Wordpress
Hint
Use string concatenation to wrap the sanitized comment in a div and echo it.
Practice
(1/5)
1. What is the main purpose of XSS prevention in WordPress?
easy
A. To improve SEO rankings
B. To speed up website loading time
C. To stop harmful scripts from running on your website
D. To add more images to your site
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 C
Quick 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
A. esc_html()
B. wp_kses_post()
C. sanitize_text_field()
D. wp_enqueue_script()
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 A
Quick 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
A. PHP error
B. (runs alert)
C. Empty output
D. <script>alert('XSS')</script> (shown as text)
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 D
Quick 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: