0
0
Wordpressframework~20 mins

Creating shortcodes in Wordpress - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shortcode 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 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');
?>
AAn error message appears because the shortcode is not registered correctly.
BNothing appears because the function does not echo the text.
CThe shortcode name 'greet' is displayed as plain text.
DThe text 'Hello, friend!' appears where the shortcode is placed.
Attempts:
2 left
💡 Hint
Remember that shortcode functions should return the content, not echo it.
📝 Syntax
intermediate
2: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');
?>
AThe code correctly sets a default color and uses the passed attribute safely.
BThe code should echo the div instead of returning it for the shortcode to work.
CThe shortcode_atts function is used incorrectly and will cause an error.
DThe esc_attr function is unnecessary and will break the output.
Attempts:
2 left
💡 Hint
Shortcode functions must return content, and shortcode_atts helps set defaults.
🔧 Debug
advanced
2: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');
?>
ABecause add_shortcode is called after the shortcode is used, so it is not registered.
BBecause the function echoes output instead of returning it, the shortcode outputs nothing.
CBecause the shortcode name 'showdate' is invalid and not recognized.
DBecause the date function is used incorrectly and returns an empty string.
Attempts:
2 left
💡 Hint
Shortcode functions must return content, not echo it.
state_output
advanced
2: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');
?>
AOuter [inner]
B[outer]
COuter Inner
DInner Outer
Attempts:
2 left
💡 Hint
do_shortcode processes nested shortcodes inside the returned string.
🧠 Conceptual
expert
2: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');
?>
AA PHP warning about array to string conversion and the output shows 'Array'.
BThe shortcode outputs 'HelloWorld' by concatenating array elements.
CThe shortcode outputs nothing silently without error.
DA fatal error occurs stopping the page from loading.
Attempts:
2 left
💡 Hint
Shortcodes must return strings, not arrays.