How to create custom shortcodes on WooCommerce
Create custom WooCommerce shortcodes by adding PHP functions to your theme's functions.php file or a custom plugin, then register them using WordPress's add_shortcode() function. These shortcodes can display products, categories, or custom WooCommerce data anywhere on your site.
Prerequisites
- WordPress admin access
- Basic PHP knowledge
- Active WooCommerce plugin
- Child theme or custom plugin setup
Step-by-Step Instructions
Access your theme's functions.php file
functions.php file. Alternatively, access it via FTP or cPanel file manager at /wp-content/themes/your-theme/functions.php. Always use a child theme to prevent losing customizations during theme updates.Create the shortcode function
function custom_woo_products_shortcode($atts) {
$atts = shortcode_atts(array(
'limit' => 4,
'category' => '',
'orderby' => 'date'
), $atts);
// Your WooCommerce query logic here
ob_start();
// Output your content
return ob_get_clean();
}Register the shortcode
add_shortcode('custom_products', 'custom_woo_products_shortcode');Replace
custom_products with your desired shortcode name and custom_woo_products_shortcode with your function name. The shortcode name should be unique and descriptive.Build the WooCommerce query
$args = array(
'post_type' => 'product',
'posts_per_page' => $atts['limit'],
'orderby' => $atts['orderby']
);
if (!empty($atts['category'])) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category']
)
);
}
$products = new WP_Query($args);Create the output HTML
if ($products->have_posts()) {
echo '';
while ($products->have_posts()) {
$products->the_post();
global $product;
echo '';
echo '' . get_the_title() . '
';
echo '' . $product->get_price_html() . '
';
echo '';
}
echo '';
wp_reset_postdata();
}Add CSS styling
.custom-woo-products {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin: 20px 0;
}
.product-item {
border: 1px solid #ddd;
padding: 15px;
text-align: center;
}Test your shortcode
[custom_products limit="6" category="electronics" orderby="price"]. Click Preview or Update to see the results. Test different attribute combinations to ensure your shortcode works as expected. You can also use the shortcode in widgets or template files using do_shortcode().Common Issues & Troubleshooting
Shortcode displays code instead of output
Ensure you're using return instead of echo in your shortcode function. Use ob_start() and ob_get_clean() to capture and return output properly.
Products not displaying or showing wrong products
Check your WP_Query arguments and ensure product categories exist. Verify that products are published and in stock. Add var_dump($products->found_posts) to debug query results.
Fatal error or white screen after adding code
Check for PHP syntax errors in your functions.php file. Remove the recently added code via FTP or cPanel file manager. Enable WordPress debug mode to see specific error messages.
Shortcode attributes not working
Ensure you're using shortcode_atts() to properly merge default and user-provided attributes. Check attribute names match exactly between the shortcode call and your function definition.