Bird
Raised Fist0
Wordpressframework~20 mins

Data escaping (output) in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
WordPress Escaping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this WordPress escaping function?
Consider the following PHP code snippet in a WordPress theme template:

<?php
$user_input = '<script>alert("XSS")</script>';
echo esc_html( $user_input );
?>

What will be the output rendered in the browser?
Wordpress
<?php
$user_input = '<script>alert("XSS")</script>';
echo esc_html( $user_input );
?>
A&amp;lt;script&amp;gt;alert(&amp;quot;XSS&amp;quot;)&amp;lt;/script&amp;gt;
B&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;
Calert("XSS")
D<script>alert("XSS")</script>
Attempts:
2 left
💡 Hint
esc_html() converts special HTML characters to safe entities.
📝 Syntax
intermediate
1:30remaining
Which option correctly escapes a URL for safe output in WordPress?
You want to safely output a URL stored in $url variable inside an HTML attribute in WordPress. Which of the following is the correct escaping function to use?
Aecho sanitize_text_field( $url );
Becho esc_html( $url );
Cecho esc_attr( $url );
Decho esc_url( $url );
Attempts:
2 left
💡 Hint
Use the function designed specifically for URLs.
🔧 Debug
advanced
2:30remaining
Why does this WordPress code output raw HTML instead of escaped text?
Given this code:

<?php
$content = '<strong>Hello</strong>';
echo esc_attr( $content );
?>

Why does the browser render Hello as bold text instead of showing the tags as text?
Wordpress
<?php
$content = '<strong>Hello</strong>';
echo esc_attr( $content );
?>
Aesc_attr() escapes for HTML attributes, but echo outputs raw HTML in body, so tags render.
Besc_attr() does not escape HTML tags, so they render as HTML.
CThe variable $content is not escaped at all, causing raw HTML output.
Desc_attr() escapes HTML tags but echo removes escaping automatically.
Attempts:
2 left
💡 Hint
esc_attr() is for attribute values, not general HTML content.
❓ state_output
advanced
2:30remaining
What is the output of this WordPress code with nested escaping?
Analyze this code:

<?php
$data = '<a href="http://example.com">Link</a>';
echo esc_html( esc_attr( $data ) );
?>

What will be displayed in the browser?
Wordpress
<?php
$data = '<a href="http://example.com">Link</a>';
echo esc_html( esc_attr( $data ) );
?>
A&lt;a href=&quot;http://example.com&quot;&gt;Link&lt;/a&gt;
B<a href="http://example.com">Link</a>
C&amp;lt;a href=&amp;quot;http://example.com&amp;quot;&amp;gt;Link&amp;lt;/a&amp;gt;
Dhttp://example.com
Attempts:
2 left
💡 Hint
esc_attr() escapes quotes and special chars, esc_html() escapes HTML entities again.
🧠 Conceptual
expert
3:00remaining
Which WordPress escaping function should you use to safely output user input inside a JavaScript block?
You want to output user input safely inside a JavaScript block in a WordPress theme. Which escaping function is the best choice?
Aesc_js()
Besc_html()
Cesc_attr()
Desc_url()
Attempts:
2 left
💡 Hint
Think about escaping for JavaScript context, not HTML or URLs.

Practice

(1/5)
1. What is the main purpose of using data escaping functions like esc_html() in WordPress?
easy
A. To clean output and protect the site from harmful code
B. To speed up the website loading time
C. To change the color of text on the page
D. To store data in the database securely

Solution

  1. Step 1: Understand what data escaping does

    Data escaping cleans output by converting special characters to safe ones to prevent harmful code execution.
  2. Step 2: Identify the purpose in WordPress context

    WordPress uses escaping functions to protect the site from security risks like cross-site scripting (XSS).
  3. Final Answer:

    To clean output and protect the site from harmful code -> Option A
  4. Quick Check:

    Data escaping = Protect site [OK]
Hint: Escaping means cleaning output to keep site safe [OK]
Common Mistakes:
  • Thinking escaping speeds up the site
  • Confusing escaping with styling
  • Believing escaping stores data securely
2. Which of the following is the correct way to escape a URL before outputting it in WordPress?
easy
A. echo esc_html($url);
B. echo esc_url($url);
C. echo esc_attr($url);
D. echo sanitize_text_field($url);

Solution

  1. Step 1: Identify the correct escaping function for URLs

    WordPress provides esc_url() specifically to clean URLs before output.
  2. Step 2: Compare with other functions

    esc_html() is for HTML content, esc_attr() for attributes, and sanitize_text_field() is for input sanitizing, not output escaping.
  3. Final Answer:

    echo esc_url($url); -> Option B
  4. Quick Check:

    Escape URL with esc_url() [OK]
Hint: Use esc_url() for URLs, esc_attr() for attributes [OK]
Common Mistakes:
  • Using esc_html() for URLs
  • Using sanitize_text_field() for output escaping
  • Not escaping URLs at all
3. What will be the output of this code snippet if $user_input = '<script>alert(1)</script>'; and we run echo esc_html($user_input);?
medium
A. <script>alert(1)</script>
B.
C. &lt;script&gt;alert(1)&lt;/script&gt;
D. alert(1)

Solution

  1. Step 1: Understand what esc_html() does to special characters

    It converts special HTML characters like <, > to their safe encoded forms (<, >).
  2. Step 2: Apply escaping to the input string

    The input already has < and > but esc_html() will convert the ampersands (&) to &, so the output becomes &lt;script&gt;alert(1)&lt;/script&gt;.
  3. Final Answer:

    &lt;script&gt;alert(1)&lt;/script&gt; -> Option C
  4. Quick Check:

    esc_html() encodes & to & [OK]
Hint: esc_html() converts & to & and < > to < > [OK]
Common Mistakes:
  • Assuming esc_html() outputs raw HTML
  • Ignoring double escaping of &
  • Confusing esc_html() with esc_attr()
4. You see this code in a WordPress theme: echo $user_name; where $user_name comes from user input. What is the problem and how to fix it?
medium
A. No problem, output is safe as is
B. Missing escaping, fix with echo sanitize_text_field($user_name);
C. Missing escaping, fix with echo esc_url($user_name);
D. Missing escaping, fix with echo esc_html($user_name);

Solution

  1. Step 1: Identify the security risk

    Outputting user input directly can cause security issues like XSS attacks.
  2. Step 2: Choose the correct escaping function for HTML output

    Since the output is HTML content, esc_html() is the proper function to escape special characters safely.
  3. Final Answer:

    Missing escaping, fix with echo esc_html($user_name); -> Option D
  4. Quick Check:

    Escape user input with esc_html() for HTML output [OK]
Hint: Always escape user input before echoing in HTML [OK]
Common Mistakes:
  • Not escaping user input at all
  • Using esc_url() for plain text
  • Using sanitize_text_field() only sanitizes input, not escapes output
5. You want to output a user-submitted URL inside an HTML attribute like <a href="...">. Which is the best way to escape it to keep your site safe and valid?
hard
A. echo esc_url($user_url);
B. echo esc_html($user_url);
C. echo esc_attr($user_url);
D. echo esc_url(esc_attr($user_url));

Solution

  1. Step 1: Understand context of output

    The URL is inside an HTML attribute (href), so it needs URL-specific escaping and validation.
  2. Step 2: Choose correct escaping function for URL attributes

    esc_url() sanitizes URL schemes (e.g., strips javascript:) and escapes for safe HTML attribute output.
  3. Step 3: Why not esc_attr()?

    esc_attr() only escapes HTML chars but allows dangerous schemes like javascript:alert(1), leading to XSS.
  4. Final Answer:

    echo esc_url($user_url); -> Option A
  5. Quick Check:

    esc_url() for href attributes [OK]
Hint: esc_url() for links in href/src attributes [OK]
Common Mistakes:
  • Using esc_attr() which doesn't validate URL schemes
  • Using esc_html() for attributes
  • Double escaping with esc_url(esc_attr())
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard