0
0
Wordpressframework~20 mins

Enclosing shortcodes in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enclosing Shortcodes 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 enclosing shortcode?
Consider this WordPress shortcode definition and usage. What will be the output on the page?
Wordpress
[code]
function wrap_shortcode($atts, $content = null) {
  return '<div class="wrapper">' . strtoupper($content) . '</div>';
}
add_shortcode('wrap', 'wrap_shortcode');

// Usage in post content:
// [wrap]hello world[/wrap]
[/code]
A<div class="wrapper">HELLO WORLD</div>
B<div class="wrapper">hello world</div>
C[wrap]hello world[/wrap]
DError: Missing content parameter
Attempts:
2 left
💡 Hint
Remember that the $content parameter contains the text between the shortcode tags.
📝 Syntax
intermediate
2:00remaining
Which shortcode definition correctly supports enclosing content?
You want to create a shortcode that can wrap content between opening and closing tags. Which of these definitions correctly supports that?
Afunction my_shortcode($atts) { return 'Hello'; } add_shortcode('greet', 'my_shortcode');
Bfunction my_shortcode() { return 'Hello'; } add_shortcode('greet', 'my_shortcode');
Cfunction my_shortcode($atts, $content = null) { return strtoupper($content); } add_shortcode('greet', 'my_shortcode');
Dfunction my_shortcode($content) { return strtolower($content); } add_shortcode('greet', 'my_shortcode');
Attempts:
2 left
💡 Hint
Enclosing shortcodes require a second parameter for content with a default null value.
🔧 Debug
advanced
2:00remaining
Why does this enclosing shortcode output nothing?
Given this shortcode code, why does the content not appear on the page? [code] function highlight_shortcode($atts, $content) { return '' . $content . ''; } add_shortcode('highlight', 'highlight_shortcode'); // Usage: [highlight]Important[/highlight] [/code]
ABecause $content parameter is missing the default null value, so it is empty.
BBecause the content is not passed as the second parameter with default null, so $content is empty.
CBecause the shortcode is not registered with add_shortcode.
DBecause the shortcode function does not return anything.
Attempts:
2 left
💡 Hint
Check the function parameters and how WordPress passes content to shortcodes.
state_output
advanced
2:00remaining
What is the output of nested enclosing shortcodes?
Given these shortcode definitions and usage, what is the final output? [code] function bold_shortcode($atts, $content = null) { return '' . $content . ''; } function italic_shortcode($atts, $content = null) { return '' . $content . ''; } add_shortcode('bold', 'bold_shortcode'); add_shortcode('italic', 'italic_shortcode'); // Usage: // [bold]This is [italic]important[/italic][/bold] [/code]
A<strong>This is <em>important</em></strong>
B[bold]This is [italic]important[/italic][/bold]
C<em>This is <strong>important</strong></em>
DThis is important
Attempts:
2 left
💡 Hint
Shortcodes are processed inside out, so inner shortcode output is passed as content to outer shortcode.
🧠 Conceptual
expert
2:00remaining
What happens if an enclosing shortcode returns null?
If a WordPress enclosing shortcode function returns null instead of a string, what will be the result on the page?
AThe content inside the shortcode is displayed without any wrapping.
BThe shortcode tags remain visible as plain text on the page.
CWordPress throws a fatal error and stops rendering the page.
DThe shortcode is replaced by an empty string, so no visible output.
Attempts:
2 left
💡 Hint
Think about how WordPress handles shortcode return values that are not strings.