Introduction
WordPress shortcode on a page or post will be replaced with some content, and the process that happened in the background is as follows:
When WordPress “runs into” a shortcode it is instructed to look for a macro inside the brackets [ ], then the macro calls a callback function that will replace the shortcode with some dynamic content.
Enable shortcode everywhere
By default, WordPress ignores the shortcode if it finds it somewhere other than the part where the content of the post or page is entered. In order to activate shortcodes in other areas as well, it is necessary to add an appropriate filter to functions.php
Shortcode in widget
|
|
add_filter('widget_text', 'do_shortcode'); |
Now it is enough to put the shortcode in a regular Text widget and it in the area you want to display the content of the shortcode.
Shortcode in comments
|
|
add_filter( 'comment_text', 'do_shortcode' ); |
Shortcode in excerpt
|
|
add_filter( 'the_excerpt', 'do_shortcode'); |
Shortcode in topic header
If for some reason we want to insert a shortcode inside a template or plugin, e.g. “custom page template” we use the function do_shortcode( $content )
Read more