Bird
0
0

Which of the following is the correct syntax to define a shortcode with a parameter named color in WordPress PHP?

easy📝 Syntax Q12 of 15
Wordpress - Shortcodes and Blocks
Which of the following is the correct syntax to define a shortcode with a parameter named color in WordPress PHP?
Afunction my_shortcode($atts) { return $atts['color']; }
Bfunction my_shortcode($color) { return $color; }
Cfunction my_shortcode() { return $color; }
Dfunction my_shortcode($atts) { $atts = shortcode_atts(['color' => 'blue'], $atts); return $atts['color']; }
Step-by-Step Solution
Solution:
  1. Step 1: Recognize correct use of shortcode_atts()

    The function shortcode_atts() safely sets default values and merges user parameters.
  2. Step 2: Check parameter handling

    function my_shortcode($atts) { $atts = shortcode_atts(['color' => 'blue'], $atts); return $atts['color']; } uses shortcode_atts() with default 'color' and returns the value correctly.
  3. Final Answer:

    function my_shortcode($atts) { $atts = shortcode_atts(['color' => 'blue'], $atts); return $atts['color']; } -> Option D
  4. Quick Check:

    Use shortcode_atts() to handle parameters [OK]
Quick Trick: Always use shortcode_atts() to set defaults [OK]
Common Mistakes:
  • Not using shortcode_atts() for defaults
  • Missing parameter array in function
  • Trying to access parameters without $atts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes