A shortcode is a simple code wrapped in square brackets that you can add to posts or pages to insert dynamic content without writing full code.
Click to reveal answer
beginner
How do parameters work in WordPress shortcodes?
Parameters let you customize the shortcode output by passing extra information inside the shortcode, like [gallery size="medium"].
Click to reveal answer
beginner
Example: What does this shortcode do? [button color="red" size="large"]Click me[/button]
It creates a button with red color and large size. The parameters 'color' and 'size' change how the button looks.
Click to reveal answer
intermediate
How do you define a shortcode with parameters in PHP?
You create a function that accepts attributes as an array, use shortcode_atts() to set defaults, then register it with add_shortcode().
Click to reveal answer
intermediate
Why use shortcode_atts() in shortcode functions?
It sets default values for parameters and merges them with user input, so your shortcode always has valid data to work with.
Click to reveal answer
What symbol wraps a WordPress shortcode?
ACurly braces {}
BSquare brackets []
CParentheses ()
DAngle brackets <>
✗ Incorrect
Shortcodes are always wrapped in square brackets like [shortcode].
How do you pass parameters to a shortcode?
AInside the shortcode like [shortcode param="value"]
BAs a separate PHP file
CUsing HTML attributes
DBy editing the WordPress core
✗ Incorrect
Parameters go inside the shortcode tag as key="value" pairs.
Which function helps set default parameter values in shortcode functions?
Aget_shortcode_defaults()
Badd_shortcode()
Cshortcode_atts()
Dregister_shortcode()
✗ Incorrect
shortcode_atts() merges user parameters with defaults.
What does add_shortcode() do?
ARegisters a shortcode and links it to a function
BAdds a new WordPress plugin
CCreates a new post
DDeletes a shortcode
✗ Incorrect
add_shortcode() tells WordPress what function to run for a shortcode.
If a shortcode has [button color="blue"], what is 'color'?
AA WordPress theme
BThe shortcode name
CA PHP function
DA parameter that customizes the shortcode
✗ Incorrect
'color' is a parameter passed to the shortcode to change its output.
Explain how to create a WordPress shortcode that accepts parameters and uses default values.
Think about how to handle missing parameters and connect the shortcode name to the function.
You got /4 concepts.
Describe how parameters inside a shortcode affect its behavior and give a simple example.
Imagine changing the color or size of a button using parameters.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using parameters in WordPress shortcodes?
easy
A. To customize the output of the shortcode dynamically
B. To add CSS styles directly inside the shortcode
C. To create new WordPress posts automatically
D. To disable the shortcode temporarily
Solution
Step 1: Understand shortcode parameters
Parameters allow passing extra information to shortcodes to change their behavior or output.
Step 2: Identify the purpose of parameters
They let you customize what the shortcode shows without changing the code itself.
Final Answer:
To customize the output of the shortcode dynamically -> Option A
Quick Check:
Parameters customize shortcode output [OK]
Hint: Parameters change shortcode output without code edits [OK]
Common Mistakes:
Thinking parameters add styles directly
Confusing parameters with creating posts
Believing parameters disable shortcodes
2. Which of the following is the correct syntax to define a shortcode with a parameter named color in WordPress PHP?
easy
A. function my_shortcode($atts) { return $atts['color']; }
B. function my_shortcode($color) { return $color; }
C. function my_shortcode() { return $color; }
D. function my_shortcode($atts) { $atts = shortcode_atts(['color' => 'blue'], $atts); return $atts['color']; }
Solution
Step 1: Recognize correct use of shortcode_atts()
The function shortcode_atts() safely sets default values and merges user parameters.
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.
Final Answer:
function my_shortcode($atts) { $atts = shortcode_atts(['color' => 'blue'], $atts); return $atts['color']; } -> Option D
Quick Check:
Use shortcode_atts() to handle parameters [OK]
Hint: Always use shortcode_atts() to set defaults [OK]
What will be the output of [greet name="Alice"] in a post?
medium
A. Hello, Guest!
B. Hello, name!
C. Hello, Alice!
D. Hello, !
Solution
Step 1: Understand shortcode_atts default and override
The default name is 'Guest', but if 'name' is passed, it overrides the default.
Step 2: Check the passed parameter
The shortcode passes name="Alice", so $atts['name'] becomes 'Alice'.
Final Answer:
Hello, Alice! -> Option C
Quick Check:
Parameter overrides default value [OK]
Hint: Passed parameters override defaults in shortcode_atts() [OK]
Common Mistakes:
Ignoring passed parameters and using defaults
Misreading the parameter key
Expecting output without parameter value
4. What is wrong with this shortcode handler code?
function example_shortcode($atts) {
return "Color is " . $atts['color'];
}
Used as [example color="red"]
medium
A. It will always return 'Color is ' without the color value
B. It will cause an undefined index error if 'color' is not passed
C. It will not register the shortcode properly
D. It will output the color but ignore other parameters
Solution
Step 1: Check parameter handling without defaults
The code accesses $atts['color'] directly without setting defaults or checking if it exists.
Step 2: Identify error risk
If the shortcode is used without 'color', PHP will throw an undefined index notice or error.
Final Answer:
It will cause an undefined index error if 'color' is not passed -> Option B
Quick Check:
Always use shortcode_atts() to avoid undefined indexes [OK]
Hint: Use shortcode_atts() to prevent undefined index errors [OK]
Common Mistakes:
Accessing parameters without defaults
Assuming parameters always exist
Ignoring PHP notices or warnings
5. You want to create a shortcode [box color="green" size="large"] that outputs a colored box with size text. Which PHP function correctly handles both parameters with defaults and returns a string like <div class='box large' style='background:green;'>Box</div>?
hard
A. function box_shortcode($atts) {
$atts = shortcode_atts(['color' => 'blue', 'size' => 'medium'], $atts);
return "
Box
";
}
B. function box_shortcode($color, $size) {
return "
Box
";
}
C. function box_shortcode() {
return "
Box
";
}
D. function box_shortcode($atts) {
return "
Box
";
}
Solution
Step 1: Use shortcode_atts() to set defaults for 'color' and 'size'