Challenge - 5 Problems
Shortcode Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this shortcode function?
Given this WordPress shortcode function, what will be displayed on the page when the shortcode is used?
Wordpress
<?php
function greet_shortcode() {
return 'Hello, friend!';
}
add_shortcode('greet', 'greet_shortcode');
?>Attempts:
2 left
💡 Hint
Remember that shortcode functions should return the content, not echo it.
✗ Incorrect
The shortcode function returns the string 'Hello, friend!'. WordPress replaces the shortcode with this returned string in the page content.
📝 Syntax
intermediate2:00remaining
Which option correctly registers a shortcode with an attribute?
You want to create a shortcode [colorbox color="red"] that outputs a colored box. Which code correctly handles the color attribute?
Wordpress
<?php
function colorbox_shortcode($atts) {
$atts = shortcode_atts(array(
'color' => 'blue'
), $atts);
return '<div style="background-color:' . esc_attr($atts['color']) . '; width:100px; height:100px;"></div>';
}
add_shortcode('colorbox', 'colorbox_shortcode');
?>Attempts:
2 left
💡 Hint
Shortcode functions must return content, and shortcode_atts helps set defaults.
✗ Incorrect
This code uses shortcode_atts to set a default color and safely outputs the div with the color style. Returning the string is correct for shortcodes.
🔧 Debug
advanced2:00remaining
Why does this shortcode output nothing?
Consider this shortcode code. Why does it not display anything on the page?
Wordpress
<?php
function show_date_shortcode() {
echo date('Y-m-d');
}
add_shortcode('showdate', 'show_date_shortcode');
?>Attempts:
2 left
💡 Hint
Shortcode functions must return content, not echo it.
✗ Incorrect
Shortcodes require the function to return the content string. Echoing outputs content immediately and returns nothing, so the shortcode outputs empty.
❓ state_output
advanced2:00remaining
What will be the output of this shortcode with nested shortcodes?
Given these shortcode functions, what is the final output of [outer] when used in a post?
Wordpress
<?php
function inner_shortcode() {
return 'Inner';
}
function outer_shortcode() {
return 'Outer ' . do_shortcode('[inner]');
}
add_shortcode('inner', 'inner_shortcode');
add_shortcode('outer', 'outer_shortcode');
?>Attempts:
2 left
💡 Hint
do_shortcode processes nested shortcodes inside the returned string.
✗ Incorrect
The outer shortcode returns 'Outer ' plus the result of processing the inner shortcode, which returns 'Inner'. So the final output is 'Outer Inner'.
🧠 Conceptual
expert2:00remaining
What error occurs if a shortcode function returns an array instead of a string?
If a shortcode function returns an array like ['Hello', 'World'], what happens when the shortcode is used in WordPress content?
Wordpress
<?php
function array_shortcode() {
return ['Hello', 'World'];
}
add_shortcode('arraytest', 'array_shortcode');
?>Attempts:
2 left
💡 Hint
Shortcodes must return strings, not arrays.
✗ Incorrect
Returning an array causes PHP to warn about converting array to string and outputs the word 'Array' in place of the shortcode.