๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—–๐—ฟ๐—ฒ๐—ฎ๐˜๐—ฒ ๐—–๐˜‚๐˜€๐˜๐—ผ๐—บ ๐—ฆ๐—ต๐—ผ๐—ฟ๐˜๐—ฐ๐—ผ๐—ฑ๐—ฒ๐˜€ ๐—ณ๐—ผ๐—ฟ ๐——๐˜†๐—ป๐—ฎ๐—บ๐—ถ๐—ฐ ๐—–๐—ผ๐—ป๐˜๐—ฒ๐—ป๐˜ ๐—ถ๐—ป ๐—ช๐—ผ๐—ฟ๐—ฑ๐—ฃ๐—ฟ๐—ฒ๐˜€๐˜€

hortcodes are a fantastic way to add reusable content or functionality to WordPress sites with minimal effort. Today, letโ€™s explore how to create custom shortcodes that display dynamic content, such as user data or recent posts.

๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿญ: ๐—ฅ๐—ฒ๐—ด๐—ถ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฎ ๐—•๐—ฎ๐˜€๐—ถ๐—ฐ ๐—ฆ๐—ต๐—ผ๐—ฟ๐˜๐—ฐ๐—ผ๐—ฑ๐—ฒ
In your themeโ€™s ๐˜ง๐˜ถ๐˜ฏ๐˜ค๐˜ต๐˜ช๐˜ฐ๐˜ฏ๐˜ด.๐˜ฑ๐˜ฉ๐˜ฑ file or a custom plugin or mu-plugin, you can register a shortcode using the ๐˜ข๐˜ฅ๐˜ฅ_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆ function.

๐˜ง๐˜ถ๐˜ฏ๐˜ค๐˜ต๐˜ช๐˜ฐ๐˜ฏ ๐˜ฎ๐˜บ_๐˜ค๐˜ถ๐˜ด๐˜ต๐˜ฐ๐˜ฎ_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆ() {
๐˜ณ๐˜ฆ๐˜ต๐˜ถ๐˜ณ๐˜ฏ โ€œ๐˜๐˜ฆ๐˜ญ๐˜ญ๐˜ฐ, ๐˜ž๐˜ฐ๐˜ณ๐˜ฅ๐˜—๐˜ณ๐˜ฆ๐˜ด๐˜ด!โ€;
}
๐˜ข๐˜ฅ๐˜ฅ_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆ(โ€˜๐˜จ๐˜ณ๐˜ฆ๐˜ฆ๐˜ต๐˜ช๐˜ฏ๐˜จโ€™, โ€˜๐˜ฎ๐˜บ_๐˜ค๐˜ถ๐˜ด๐˜ต๐˜ฐ๐˜ฎ_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆโ€™);

Now, simply add [๐˜จ๐˜ณ๐˜ฆ๐˜ฆ๐˜ต๐˜ช๐˜ฏ๐˜จ] to any post or page, and WordPress will output โ€œHello, WordPress!โ€ ๐Ÿš€

๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿฎ: ๐—”๐—ฑ๐—ฑ ๐——๐˜†๐—ป๐—ฎ๐—บ๐—ถ๐—ฐ ๐—–๐—ผ๐—ป๐˜๐—ฒ๐—ป๐˜
Next, letโ€™s modify the shortcode to display dynamic content, such as the current userโ€™s name.

๐˜ง๐˜ถ๐˜ฏ๐˜ค๐˜ต๐˜ช๐˜ฐ๐˜ฏ ๐˜ถ๐˜ด๐˜ฆ๐˜ณ_๐˜จ๐˜ณ๐˜ฆ๐˜ฆ๐˜ต๐˜ช๐˜ฏ๐˜จ_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆ() {
$๐˜ค๐˜ถ๐˜ณ๐˜ณ๐˜ฆ๐˜ฏ๐˜ต_๐˜ถ๐˜ด๐˜ฆ๐˜ณ = ๐˜ธ๐˜ฑ_๐˜จ๐˜ฆ๐˜ต_๐˜ค๐˜ถ๐˜ณ๐˜ณ๐˜ฆ๐˜ฏ๐˜ต_๐˜ถ๐˜ด๐˜ฆ๐˜ณ();
๐˜ณ๐˜ฆ๐˜ต๐˜ถ๐˜ณ๐˜ฏ โ€˜๐˜๐˜ฆ๐˜ญ๐˜ญ๐˜ฐ, โ€˜ . ๐˜ฆ๐˜ด๐˜ค_๐˜ฉ๐˜ต๐˜ฎ๐˜ญ($๐˜ค๐˜ถ๐˜ณ๐˜ณ๐˜ฆ๐˜ฏ๐˜ต_๐˜ถ๐˜ด๐˜ฆ๐˜ณ->๐˜ฅ๐˜ช๐˜ด๐˜ฑ๐˜ญ๐˜ข๐˜บ_๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ) . โ€˜!โ€™;
}
๐˜ข๐˜ฅ๐˜ฅ_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆ(โ€˜๐˜ถ๐˜ด๐˜ฆ๐˜ณ_๐˜จ๐˜ณ๐˜ฆ๐˜ฆ๐˜ต๐˜ช๐˜ฏ๐˜จโ€™, โ€˜๐˜ถ๐˜ด๐˜ฆ๐˜ณ_๐˜จ๐˜ณ๐˜ฆ๐˜ฆ๐˜ต๐˜ช๐˜ฏ๐˜จ_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆโ€™);

This will greet logged-in users by name. If the user is not logged in, you can add logic to show a generic greeting.

๐—ฆ๐˜๐—ฒ๐—ฝ ๐Ÿฏ: ๐—จ๐˜€๐—ฒ ๐—ฆ๐—ต๐—ผ๐—ฟ๐˜๐—ฐ๐—ผ๐—ฑ๐—ฒ ๐—”๐˜๐˜๐—ฟ๐—ถ๐—ฏ๐˜‚๐˜๐—ฒ๐˜€
You can extend shortcodes by adding attributes to make them more flexible. For example, letโ€™s create a shortcode that displays recent posts with a customizable post count.

๐˜ง๐˜ถ๐˜ฏ๐˜ค๐˜ต๐˜ช๐˜ฐ๐˜ฏ ๐˜ณ๐˜ฆ๐˜ค๐˜ฆ๐˜ฏ๐˜ต_๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ด_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆ($๐˜ข๐˜ต๐˜ต๐˜ด) {
$๐˜ข๐˜ต๐˜ต๐˜ด = ๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆ_๐˜ข๐˜ต๐˜ต๐˜ด(
๐˜ข๐˜ณ๐˜ณ๐˜ข๐˜บ(
โ€˜๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ตโ€™ => 5,
),
$๐˜ข๐˜ต๐˜ต๐˜ด,
โ€˜๐˜ณ๐˜ฆ๐˜ค๐˜ฆ๐˜ฏ๐˜ต_๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ดโ€™
);

$๐˜ณ๐˜ฆ๐˜ค๐˜ฆ๐˜ฏ๐˜ต_๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ด = ๐˜ธ๐˜ฑ_๐˜จ๐˜ฆ๐˜ต_๐˜ณ๐˜ฆ๐˜ค๐˜ฆ๐˜ฏ๐˜ต_๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ด(๐˜ข๐˜ณ๐˜ณ๐˜ข๐˜บ(โ€˜๐˜ฏ๐˜ถ๐˜ฎ๐˜ฃ๐˜ฆ๐˜ณ๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ดโ€™ => $๐˜ข๐˜ต๐˜ต๐˜ด[โ€˜๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ตโ€™]));
$๐˜ฐ๐˜ถ๐˜ต๐˜ฑ๐˜ถ๐˜ต = โ€˜<๐˜ถ๐˜ญ>โ€™;
๐˜ง๐˜ฐ๐˜ณ๐˜ฆ๐˜ข๐˜ค๐˜ฉ ($๐˜ณ๐˜ฆ๐˜ค๐˜ฆ๐˜ฏ๐˜ต_๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ด ๐˜ข๐˜ด $๐˜ฑ๐˜ฐ๐˜ด๐˜ต) {
$๐˜ฐ๐˜ถ๐˜ต๐˜ฑ๐˜ถ๐˜ต .= โ€˜<๐˜ญ๐˜ช>โ€™ . ๐˜ฆ๐˜ด๐˜ค_๐˜ฉ๐˜ต๐˜ฎ๐˜ญ($๐˜ฑ๐˜ฐ๐˜ด๐˜ต[โ€˜๐˜ฑ๐˜ฐ๐˜ด๐˜ต_๐˜ต๐˜ช๐˜ต๐˜ญ๐˜ฆโ€™]) . โ€˜</๐˜ญ๐˜ช>โ€™;
}
$๐˜ฐ๐˜ถ๐˜ต๐˜ฑ๐˜ถ๐˜ต .= โ€˜</๐˜ถ๐˜ญ>โ€™;

๐˜ณ๐˜ฆ๐˜ต๐˜ถ๐˜ณ๐˜ฏ $๐˜ฐ๐˜ถ๐˜ต๐˜ฑ๐˜ถ๐˜ต;
}
๐˜ข๐˜ฅ๐˜ฅ_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆ(โ€˜๐˜ณ๐˜ฆ๐˜ค๐˜ฆ๐˜ฏ๐˜ต_๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ดโ€™, โ€˜๐˜ณ๐˜ฆ๐˜ค๐˜ฆ๐˜ฏ๐˜ต_๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ด_๐˜ด๐˜ฉ๐˜ฐ๐˜ณ๐˜ต๐˜ค๐˜ฐ๐˜ฅ๐˜ฆโ€™);

Now, you can use [๐˜ณ๐˜ฆ๐˜ค๐˜ฆ๐˜ฏ๐˜ต_๐˜ฑ๐˜ฐ๐˜ด๐˜ต๐˜ด ๐˜ค๐˜ฐ๐˜ถ๐˜ฏ๐˜ต=โ€3โ€ณ] to display the latest three posts, or customize it further as needed.

๐Ÿ’ก ๐—ฃ๐—ฟ๐—ผ ๐—ง๐—ถ๐—ฝ: Shortcodes are great for theme and plugin development, providing reusable functionality without needing to edit page templates.

hashtag#WordPress hashtag#WebDevelopment hashtag#PHP hashtag#CustomShortcodes hashtag#DynamicContent

Leave a Reply

Your email address will not be published. Required fields are marked *