Performance: Creating shortcodes
This affects page load speed and rendering because shortcodes add dynamic content processing during page generation.
<?php function cached_shortcode() { $data = get_transient('cached_data'); if (!$data) { $data = file_get_contents('https://api.example.com/data'); set_transient('cached_data', $data, HOUR_IN_SECONDS); } return $data; } add_shortcode('cached', 'cached_shortcode'); ?><?php function slow_shortcode() { $data = file_get_contents('https://api.example.com/data'); return $data; } add_shortcode('slow', 'slow_shortcode'); ?>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Slow shortcode with external API call | Minimal DOM nodes | 0 reflows (server-side delay) | Blocks initial paint | [X] Bad |
| Cached shortcode output | Minimal DOM nodes | 0 reflows | Non-blocking paint | [OK] Good |
hello in WordPress?add_shortcode with first argument as shortcode tag, second as callback function.add_shortcode('hello', 'hello_function'), which is correct syntax.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?function my_shortcode() {
return 'Welcome!';
}
add_shortcode(my_shortcode, 'welcome');add_shortcode(my_shortcode, 'welcome') where my_shortcode is not quoted, so it's treated as a constant or variable, causing an error. Also, the arguments are swapped.add_shortcode('welcome', 'my_shortcode'); with both arguments as strings and in correct order.[box color="blue"]Content[/box] that wraps the content in a colored div. Which of these functions correctly implements this?Pass the test and this pattern moves unknown