Shortcodes let you add special content or features inside WordPress posts or pages easily. They save time by reusing code with a simple tag.
Creating shortcodes in Wordpress
function shortcode_function_name($atts, $content = null) {
// Process attributes and content
return 'Your output here';
}
add_shortcode('shortcode_tag', 'shortcode_function_name');The function receives attributes and optional content inside the shortcode.
Use add_shortcode to register the shortcode tag and link it to your function.
[hello] will show "Hello, world!" wherever used.function hello_shortcode() {
return 'Hello, world!';
}
add_shortcode('hello', 'hello_shortcode');[button url="https://example.com"] creates a clickable button linking to the given URL.function button_shortcode($atts) {
$atts = shortcode_atts(['url' => '#'], $atts);
return '<a href="' . esc_url($atts['url']) . '" class="btn">Click me</a>';
}
add_shortcode('button', 'button_shortcode');[wrap]content[/wrap] wraps the content inside a div with class "wrapper".function wrap_shortcode($atts, $content = null) {
return '<div class="wrapper">' . do_shortcode($content) . '</div>';
}
add_shortcode('wrap', 'wrap_shortcode');This shortcode [greet name="Alice"] will output "Hello, Alice!". If no name is given, it says "Hello, Friend!" by default.
<?php // Shortcode to display a greeting with a name attribute function greet_shortcode($atts) { $atts = shortcode_atts(['name' => 'Friend'], $atts); return 'Hello, ' . esc_html($atts['name']) . '!'; } add_shortcode('greet', 'greet_shortcode'); // Usage in WordPress post: [greet name="Alice"] ?>
Always sanitize attributes and content to keep your site safe.
Shortcodes can be nested, but use do_shortcode() inside your function to process inner shortcodes.
Test your shortcode in posts or pages to see how it looks and works.
Shortcodes let you add reusable content with simple tags inside WordPress.
Create a function that returns the output and register it with add_shortcode.
Use attributes and content to make your shortcode flexible and dynamic.
Practice
Solution
Step 1: Understand shortcode purpose
Shortcodes allow users to add reusable content or features easily inside posts or pages.Step 2: Compare with other options
Creating themes, adding CSS, or managing users are unrelated to shortcodes.Final Answer:
To insert reusable content or functionality using a simple tag -> Option DQuick Check:
Shortcodes = reusable content tags [OK]
- Confusing shortcodes with themes or CSS
- Thinking shortcodes manage users
- Assuming shortcodes are for styling only
hello in WordPress?Solution
Step 1: Recall shortcode registration syntax
The correct function isadd_shortcodewith first argument as shortcode tag, second as callback function.Step 2: Match correct argument order
add_shortcode('hello', 'hello_function'); usesadd_shortcode('hello', 'hello_function'), which is correct syntax.Final Answer:
add_shortcode('hello', 'hello_function'); -> Option CQuick Check:
add_shortcode(tag, callback) = correct [OK]
- Using register_shortcode instead of add_shortcode
- Swapping the order of arguments
- Passing function name as first argument
function greet_shortcode($atts) {
$name = $atts['name'] ?? 'Guest';
return "Hello, $name!";
}
add_shortcode('greet', 'greet_shortcode');
What will be the output of [greet name="Alice"] in a post?Solution
Step 1: Analyze shortcode attributes
The function checks if 'name' attribute exists; if yes, uses it; else defaults to 'Guest'.Step 2: Apply given shortcode with name="Alice"
The attribute 'name' is 'Alice', so output is "Hello, Alice!".Final Answer:
Hello, Alice! -> Option BQuick Check:
Attribute 'name' passed = output uses it [OK]
- Ignoring attributes and using default always
- Outputting shortcode tag instead of processed text
- Missing attribute check causing errors
function my_shortcode() {
return 'Welcome!';
}
add_shortcode(my_shortcode, 'welcome');Solution
Step 1: Check add_shortcode argument order and types
The first argument must be the shortcode tag as a string, and the second argument must be the callback function name as a string.Step 2: Identify incorrect argument usage
The code usesadd_shortcode(my_shortcode, 'welcome')wheremy_shortcodeis not quoted, so it's treated as a constant or variable, causing an error. Also, the arguments are swapped.Step 3: Correct usage
The correct call should beadd_shortcode('welcome', 'my_shortcode');with both arguments as strings and in correct order.Final Answer:
The function name should be a string in add_shortcode -> Option AQuick Check:
add_shortcode('tag', 'function') correct order and string types [OK]
- Swapping shortcode tag and function arguments
- Not quoting function name as string
- Expecting shortcode function to echo instead of return
[box color="blue"]Content[/box] that wraps the content in a colored div. Which of these functions correctly implements this?Solution
Step 1: Check function parameters for shortcode with content
Shortcode functions that handle enclosed content must accept two parameters: $atts and $content (default null).Step 2: Verify attribute handling and output
function box_shortcode($atts, $content = null) { $color = $atts['color'] ?? 'red'; return "$content"; } add_shortcode('box', 'box_shortcode'); correctly uses $atts['color'] with default, and returns a div with inline style wrapping $content.Final Answer:
Function with ($atts, $content=null) and returns colored div wrapping content -> Option AQuick Check:
Shortcode with content needs $content param [OK]
- Swapping $atts and $content parameters
- Not setting default null for $content
- Ignoring content parameter and hardcoding output
Ready to prove it?
Pass the test and this pattern moves unknown
