Bird
0
0

Which handler correctly processes parameters and content?

hard📝 component behavior Q8 of 15
Wordpress - Shortcodes and Blocks
You want to create a shortcode [highlight color="yellow"]text[/highlight] that wraps the content in a span with the specified background color. Which handler correctly processes parameters and content?
Afunction highlight_shortcode() { return "<span>Highlight</span>"; }
Bfunction highlight_shortcode($content) { return "<span style='background-color:yellow'>" . $content . "</span>"; }
Cfunction highlight_shortcode($atts) { return "<span style='background-color:{$atts['color']}'>Content</span>"; }
Dfunction highlight_shortcode($atts, $content = null) { $atts = shortcode_atts(['color' => 'yellow'], $atts); return "<span style='background-color:{$atts['color']}'>" . do_shortcode($content) . "</span>"; }
Step-by-Step Solution
Solution:
  1. Step 1: Check parameters and content

    The handler must accept $atts and $content parameters.
  2. Step 2: Use shortcode_atts for defaults

    Defaults ensure 'color' is set if not provided.
  3. Step 3: Return wrapped content

    Content is wrapped in a span with inline style using the color.
  4. Final Answer:

    function highlight_shortcode($atts, $content = null) { $atts = shortcode_atts(['color' => 'yellow'], $atts); return "" . do_shortcode($content) . ""; } correctly handles parameters and content.
  5. Quick Check:

    Use $atts, $content, and shortcode_atts [OK]
Quick Trick: Use $atts and $content with shortcode_atts [OK]
Common Mistakes:
  • Ignoring $content parameter
  • Not setting default attributes
  • Hardcoding styles without parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes