What will be the output of [greet name="Alice"] in a post?
medium
A. Hello, !
B. Hello, Alice!
C. [greet name="Alice"]
D. Hello, Guest!
Solution
Step 1: Analyze shortcode attributes
The function checks if 'name' attribute exists; if yes, uses it; else defaults to 'Guest'.
Step 2: Apply given shortcode with name="Alice"
The attribute 'name' is 'Alice', so output is "Hello, Alice!".
Final Answer:
Hello, Alice! -> Option B
Quick Check:
Attribute 'name' passed = output uses it [OK]
Hint: Attributes override defaults in shortcode functions [OK]
Common Mistakes:
Ignoring attributes and using default always
Outputting shortcode tag instead of processed text
Missing attribute check causing errors
4. Identify the error in this shortcode registration code:
function my_shortcode() {
return 'Welcome!';
}
add_shortcode(my_shortcode, 'welcome');
medium
A. The function name should be a string in add_shortcode
B. The function must echo output, not return
C. The shortcode tag and callback function arguments are swapped
D. Shortcode functions cannot have parameters
Solution
Step 1: Check add_shortcode argument order and types
The first argument must be the shortcode tag as a string, and the second argument must be the callback function name as a string.
Step 2: Identify incorrect argument usage
The code uses add_shortcode(my_shortcode, 'welcome') where my_shortcode is not quoted, so it's treated as a constant or variable, causing an error. Also, the arguments are swapped.
Step 3: Correct usage
The correct call should be add_shortcode('welcome', 'my_shortcode'); with both arguments as strings and in correct order.
Final Answer:
The function name should be a string in add_shortcode -> Option A
Quick Check:
add_shortcode('tag', 'function') correct order and string types [OK]
Hint: First argument is shortcode tag string, second is function name string [OK]
Common Mistakes:
Swapping shortcode tag and function arguments
Not quoting function name as string
Expecting shortcode function to echo instead of return
5. You want to create a shortcode [box color="blue"]Content[/box] that wraps the content in a colored div. Which of these functions correctly implements this?
hard
A. function box_shortcode($atts, $content = null) {
$color = $atts['color'] ?? 'red';
return "
$content
";
}
add_shortcode('box', 'box_shortcode');
B. function box_shortcode($content, $atts = null) {
$color = $atts['color'] ?? 'red';
return "
$content
";
}
add_shortcode('box', 'box_shortcode');
C. function box_shortcode($atts) {
$color = $atts['color'] ?? 'red';
return "
$content
";
}
add_shortcode('box', 'box_shortcode');
D. function box_shortcode() {
return "
Content
";
}
add_shortcode('box', 'box_shortcode');
Solution
Step 1: Check function parameters for shortcode with content
Shortcode functions that handle enclosed content must accept two parameters: $atts and $content (default null).