WordPress Add Shortcodes To Excerpts

I added the Shortcode API as described here, but it is only displayed in post content. How do I use shortcode in wordpress excerpt?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Wordpress
Est. reading time N/A
Shortcode is nothing but a simple set of functions for creating macro codes for use in post content. Shortcodes are written by providing a handler function. Shortcode handlers are broadly similar to WordPress filters: they accept parameters (attributes) and return a result (the shortcode output). By default shortcode will be displayed in the post content. You need to add a filter to the_excerpt function using the following syntax:


add_filter('the_excerpt', 'do_shortcode');

Note: Make sure your theme is using the_excerpt, other wise the following example will not work.

Sample example

Add the following to your functions.php

// Shortcode syntax:
//[bar]
function bar_demo( $atts ){
 return "This is a demo for wordpress shortcut.";
}
 
// add to the post 
add_shortcode( 'bar', 'bar_demo' );
 
// add to the excerpts
add_filter('the_excerpt', 'do_shortcode');

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum