0
0
Wordpressframework~20 mins

Shortcodes with parameters in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shortcode Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output of this shortcode with parameters?
Given the shortcode handler below, what will be the output when using [greet name="Alice"] in a WordPress post?
Wordpress
<?php
function greet_shortcode($atts) {
  $atts = shortcode_atts(array('name' => 'Guest'), $atts);
  return "Hello, " . $atts['name'] . "!";
}
add_shortcode('greet', 'greet_shortcode');
?>
AHello, Alice!
B[greet name="Alice"]
CHello, Guest!
DError: Undefined index 'name'
Attempts:
2 left
💡 Hint
Look at how the shortcode_atts function sets default values and overrides them with passed parameters.
📝 Syntax
intermediate
1:30remaining
Which shortcode handler code correctly handles a 'color' parameter with a default?
Choose the correct PHP shortcode handler that sets a default color of 'blue' and returns a span with that color applied.
A
function color_shortcode($atts) {
  return "&lt;span style='color: blue;'&gt;Colored Text&lt;/span&gt;";
}
B
function color_shortcode($atts) {
  $color = $atts['color'] ?: 'blue';
  return "&lt;span style='color: $color;'&gt;Colored Text&lt;/span&gt;";
}
C
function color_shortcode($atts) {
  $color = isset($atts['color']) ? $atts['color'] : 'blue';
  return "&lt;span style='color: $color;'&gt;Colored Text&lt;/span&gt;";
}
D
function color_shortcode($atts) {
  $atts = shortcode_atts(array('color' =&gt; 'blue'), $atts);
  return "&lt;span style='color: " . esc_attr($atts['color']) . ";'&gt;Colored Text&lt;/span&gt;";
}
Attempts:
2 left
💡 Hint
Use shortcode_atts to merge defaults with passed attributes safely.
🔧 Debug
advanced
2:00remaining
Why does this shortcode not display the passed 'text' parameter?
The shortcode handler below is registered for [showtext text="Hello"]. Why does it always output 'Default Text' regardless of the parameter?
Wordpress
<?php
function showtext_shortcode($atts) {
  $text = 'Default Text';
  if (isset($atts['text'])) {
    $text = $atts['text'];
  }
  return $text;
}
add_shortcode('showtext', 'showtext_shortcode');
?>
ABecause the shortcode_atts function is not used, so $atts is empty.
BBecause the shortcode is not registered properly with add_shortcode.
CBecause the shortcode is used incorrectly in the post.
DBecause $atts is always empty unless shortcode_atts is used.
Attempts:
2 left
💡 Hint
Check how the shortcode is used in the post content.
state_output
advanced
2:00remaining
What is the output of this shortcode with nested parameters?
Consider the shortcode handler below. What will be the output of [box color="red" size="large"]Content[/box]?
Wordpress
<?php
function box_shortcode($atts, $content = null) {
  $atts = shortcode_atts(array('color' => 'blue', 'size' => 'medium'), $atts);
  return "<div style='color: " . esc_attr($atts['color']) . "; font-size: " . ($atts['size'] === 'large' ? '2rem' : '1rem') . ";'>" . do_shortcode($content) . "</div>";
}
add_shortcode('box', 'box_shortcode');
?>
A<div style='color: blue; font-size: 1rem;'>Content</div>
B<div style='color: red; font-size: 2rem;'>Content</div>
C<div style='color: red; font-size: 1rem;'>Content</div>
D<div style='color: blue; font-size: 2rem;'>Content</div>
Attempts:
2 left
💡 Hint
Check how the size parameter affects font-size and how color is applied.
🧠 Conceptual
expert
1:30remaining
Which statement about shortcode parameters and security is true?
When creating shortcodes with parameters in WordPress, which practice is the most important to prevent security issues?
AAlways escape user input parameters before outputting them to prevent XSS attacks.
BNever use shortcode_atts because it can cause parameter injection.
CUse eval() to parse parameters safely.
DTrust all shortcode parameters because they come from the site admin.
Attempts:
2 left
💡 Hint
Think about how user input can affect HTML output and security.