একটি shortcode একটি ছোট bracketed tag — [shortcode] — সরাসরি একটি post বা page-এর content-এ টাইপ করা, যা পেজ render হওয়ার সময় WordPress dynamically তৈরি করা HTML দিয়ে বদলে দেয়। এটা কোড না ছোঁয়া একজন editor-কে reusable PHP output-এর একটি অংশ দেওয়ার একটি উপায়।
// functions.php
function wgh_year_shortcode() {
return date('Y');
}
add_shortcode('current_year', 'wgh_year_shortcode');
// একটি post-এ টাইপ করা: © [current_year] My Site
// পেজে render হওয়া: © 2026 My Siteএকটি shortcode একটি HTML tag-এর মতোই attribute accept করতে পারে — shortcode_atts() এগুলোকে যুক্তিসঙ্গত default-এর সাথে merge করে।
function wgh_button_shortcode($atts) {
$atts = shortcode_atts([
'text' => 'Click Here',
'url' => '#',
], $atts);
return sprintf(
'<a class="button" href="%s">%s</a>',
esc_url($atts['url']),
esc_html($atts['text'])
);
}
add_shortcode('button', 'wgh_button_shortcode');
// [button text="Buy Now" url="/shop"]একটি opening আর closing tag দিয়ে লেখা একটি shortcode এদের মাঝে যা থাকে তা এর দ্বিতীয় argument হিসেবে পায়।
function wgh_highlight_shortcode($atts, $content = null) {
return '<mark>' . esc_html($content) . '</mark>';
}
add_shortcode('highlight', 'wgh_highlight_shortcode');
// [highlight]this text gets marked[/highlight]echo না, return
একটি shortcode function-কে অবশ্যই তার HTML return করতে হবে, কখনো সরাসরি echo না — echo করা পেজের render order-এ ভুল জায়গায় output print করে, সাধারণত content-এর ভেতরে না, তার উপরে।
একটি post-এর content-এর বাইরে, একটি shortcode-কে স্পষ্টভাবে do_shortcode()-এর মধ্য দিয়ে চালাতে হয়।
<?php echo do_shortcode('[button text="Contact Us" url="/contact"]'); ?>