Bird
0
0

You want to create a shortcode [highlight color="yellow"]Text[/highlight] that wraps the content in a span with the specified color. Which function correctly implements this?

hard📝 Application 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 color. Which function correctly implements this?
Afunction highlight_shortcode() { return '<span style="color:yellow">Text</span>'; } add_shortcode('highlight', 'highlight_shortcode');
Bfunction highlight_shortcode($atts) { return '<span style="color:' . $atts['color'] . '">' . $content . '</span>'; } add_shortcode('highlight', 'highlight_shortcode');
Cfunction highlight_shortcode($content) { return '<span style="color:yellow">' . $content . '</span>'; } add_shortcode('highlight', 'highlight_shortcode');
Dfunction highlight_shortcode($atts, $content = null) { $atts = shortcode_atts(['color' => 'red'], $atts); return '<span style="color:' . esc_attr($atts['color']) . '">' . do_shortcode($content) . '</span>'; } add_shortcode('highlight', 'highlight_shortcode');
Step-by-Step Solution
Solution:
  1. Step 1: Check function parameters

    Shortcode functions that wrap content must accept $atts and $content = null.
  2. Step 2: Use shortcode_atts for defaults

    Defaults are set with shortcode_atts to handle missing attributes.
  3. Step 3: Sanitize attributes

    Use esc_attr() to safely output attribute values.
  4. Step 4: Process nested shortcodes

    Use do_shortcode() on content to allow nested shortcodes.
  5. Step 5: Return the wrapped content

    Return the span with inline style and processed content.
  6. Final Answer:

    function highlight_shortcode($atts, $content = null) { $atts = shortcode_atts(['color' => 'red'], $atts); return '' . do_shortcode($content) . ''; } add_shortcode('highlight', 'highlight_shortcode'); correctly implements all these steps.
  7. Quick Check:

    Use shortcode_atts, esc_attr, and do_shortcode for safe, nested content [OK]
Quick Trick: Use shortcode_atts, esc_attr, and do_shortcode for safe output [OK]
Common Mistakes:
  • Not accepting $content parameter
  • Not sanitizing attributes
  • Not processing nested shortcodes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes