Challenge - 5 Problems
Shortcode Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What is the output of this shortcode with parameters?
Given the shortcode handler below, what will be the output when using [greet name="Alice"] in a WordPress post?
Wordpress
<?php
function greet_shortcode($atts) {
$atts = shortcode_atts(array('name' => 'Guest'), $atts);
return "Hello, " . $atts['name'] . "!";
}
add_shortcode('greet', 'greet_shortcode');
?>Attempts:
2 left
💡 Hint
Look at how the shortcode_atts function sets default values and overrides them with passed parameters.
✗ Incorrect
The shortcode_atts function merges the default 'name' => 'Guest' with the passed attributes. Since 'name' is provided as 'Alice', it replaces the default. The function returns 'Hello, Alice!'.
📝 Syntax
intermediate1:30remaining
Which shortcode handler code correctly handles a 'color' parameter with a default?
Choose the correct PHP shortcode handler that sets a default color of 'blue' and returns a span with that color applied.
Attempts:
2 left
💡 Hint
Use shortcode_atts to merge defaults with passed attributes safely.
✗ Incorrect
Option D uses shortcode_atts which is the recommended way to handle shortcode parameters with defaults. It also escapes the attribute for security.
🔧 Debug
advanced2:00remaining
Why does this shortcode not display the passed 'text' parameter?
The shortcode handler below is registered for [showtext text="Hello"]. Why does it always output 'Default Text' regardless of the parameter?
Wordpress
<?php
function showtext_shortcode($atts) {
$text = 'Default Text';
if (isset($atts['text'])) {
$text = $atts['text'];
}
return $text;
}
add_shortcode('showtext', 'showtext_shortcode');
?>Attempts:
2 left
💡 Hint
Check how the shortcode is used in the post content.
✗ Incorrect
The shortcode handler is correct and checks for 'text' in $atts. If the shortcode is used as [showtext], no parameter is passed, so it outputs 'Default Text'. If used as [showtext text="Hello"], it outputs 'Hello'. The problem is likely the shortcode usage missing the parameter.
❓ state_output
advanced2:00remaining
What is the output of this shortcode with nested parameters?
Consider the shortcode handler below. What will be the output of [box color="red" size="large"]Content[/box]?
Wordpress
<?php
function box_shortcode($atts, $content = null) {
$atts = shortcode_atts(array('color' => 'blue', 'size' => 'medium'), $atts);
return "<div style='color: " . esc_attr($atts['color']) . "; font-size: " . ($atts['size'] === 'large' ? '2rem' : '1rem') . ";'>" . do_shortcode($content) . "</div>";
}
add_shortcode('box', 'box_shortcode');
?>Attempts:
2 left
💡 Hint
Check how the size parameter affects font-size and how color is applied.
✗ Incorrect
The shortcode_atts sets default color blue and size medium. The passed attributes override these with color 'red' and size 'large'. The font-size is '2rem' for large size. The content is wrapped inside the styled div.
🧠 Conceptual
expert1:30remaining
Which statement about shortcode parameters and security is true?
When creating shortcodes with parameters in WordPress, which practice is the most important to prevent security issues?
Attempts:
2 left
💡 Hint
Think about how user input can affect HTML output and security.
✗ Incorrect
Shortcode parameters can be manipulated by users or content editors. Escaping output (e.g., with esc_attr or esc_html) prevents cross-site scripting (XSS) attacks. Using shortcode_atts is safe and recommended. eval() is dangerous and should never be used. Trusting all input is unsafe.