Shortcodes let you add special content inside WordPress posts or pages easily. Using parameters makes them flexible so you can change what they show without changing the code.
Shortcodes with parameters in Wordpress
[shortcode_name param1="value1" param2="value2"]
[button color="red" size="large"]Click me[/button]
[greeting name="Anna"][box title="Welcome" background="blue"]Content here[/box]
This code creates a shortcode named greet_user that takes a name parameter. If no name is given, it uses 'Guest'. It outputs a simple greeting message with the name.
You can add [greet_user name="Maria"] in your post to see: Hello, Maria! Welcome to our site.
<?php // Register shortcode with parameters function greet_user_shortcode($atts) { // Set default parameter values $atts = shortcode_atts( array( 'name' => 'Guest' ), $atts, 'greet_user' ); // Return greeting message return "<p>Hello, " . esc_html($atts['name']) . "! Welcome to our site.</p>"; } add_shortcode('greet_user', 'greet_user_shortcode'); // Usage in WordPress post or page: // [greet_user name="Maria"] ?>
Always use shortcode_atts() to set default values for parameters.
Escape output with esc_html() to keep your site safe from code injection.
Parameters are optional if you provide defaults, so shortcode works even without them.
Shortcodes with parameters let you customize content easily inside WordPress posts.
Parameters are passed as key="value" pairs inside the shortcode tag.
Use PHP functions with shortcode_atts() to handle parameters safely.
Practice
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 AQuick Check:
Parameters customize shortcode output [OK]
- Thinking parameters add styles directly
- Confusing parameters with creating posts
- Believing parameters disable shortcodes
color in WordPress PHP?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 DQuick Check:
Use shortcode_atts() to handle parameters [OK]
- Not using shortcode_atts() for defaults
- Missing parameter array in function
- Trying to access parameters without $atts
function greet_shortcode($atts) {
$atts = shortcode_atts(['name' => 'Guest'], $atts);
return "Hello, " . $atts['name'] . "!";
}What will be the output of
[greet name="Alice"] in a post?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 CQuick Check:
Parameter overrides default value [OK]
- Ignoring passed parameters and using defaults
- Misreading the parameter key
- Expecting output without parameter value
function example_shortcode($atts) {
return "Color is " . $atts['color'];
}Used as
[example color="red"]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 BQuick Check:
Always use shortcode_atts() to avoid undefined indexes [OK]
- Accessing parameters without defaults
- Assuming parameters always exist
- Ignoring PHP notices or warnings
[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>?Solution
Step 1: Use shortcode_atts() to set defaults for 'color' and 'size'
function box_shortcode($atts) { $atts = shortcode_atts(['color' => 'blue', 'size' => 'medium'], $atts); return "Box"; } correctly merges defaults with passed parameters safely.Step 2: Return properly formatted HTML string with parameters
function box_shortcode($atts) { $atts = shortcode_atts(['color' => 'blue', 'size' => 'medium'], $atts); return "Box"; } uses correct string interpolation to build the div with class and style attributes.Final Answer:
function box_shortcode($atts) { $atts = shortcode_atts(['color' => 'blue', 'size' => 'medium'], $atts); return "Box"; } -> Option AQuick Check:
Use shortcode_atts() and interpolate parameters in output [OK]
- Not using shortcode_atts() for defaults
- Wrong function parameters
- Incorrect string concatenation or quotes
Ready to prove it?
Pass the test and this pattern moves unknown
