Bird
0
0

Identify the error in this shortcode registration code:

medium📝 Debug Q14 of 15
Wordpress - Shortcodes and Blocks
Identify the error in this shortcode registration code:
function my_shortcode() {
  return 'Welcome!';
}
add_shortcode(my_shortcode, 'welcome');
AThe function name should be a string in add_shortcode
BThe function must echo output, not return
CThe shortcode tag and callback function arguments are swapped
DShortcode functions cannot have parameters
Step-by-Step Solution
Solution:
  1. 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.
  2. 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.
  3. Step 3: Correct usage

    The correct call should be add_shortcode('welcome', 'my_shortcode'); with both arguments as strings and in correct order.
  4. Final Answer:

    The function name should be a string in add_shortcode -> Option A
  5. Quick Check:

    add_shortcode('tag', 'function') correct order and string types [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes