WordPress shortcode is generally used within the_content parameters and within the loop. However, there are times when we need to be able to use shortcode in widgets and even in template files outside of the loop. I’m going to show you how to do just that!
Executing WordPress Shortcode in Widgets
This one is relatively simple. Copy and paste this line of code into your functions.php
Executing WordPress Shortcode in Template Files
This code is a slightly more difficult, but only in the sense of you having to know what shortcode you want to execute. Copy and paste the following code snippet into your functions.php
Executing WordPress Shortcode in Widgets
This one is relatively simple. Copy and paste this line of code into your functions.php
add_filter( 'widget_text', 'do_shortcode' );
And there you have it – you can now insert WordPress shortcode into your widgets!
Executing WordPress Shortcode in Template Files
This code is a slightly more difficult, but only in the sense of you having to know what shortcode you want to execute. Copy and paste the following code snippet into your functions.php
echo do_shortcode( '[shortcode title here]' );
Simply replace “shortcode title here” with your specific shortcode. You can put this anywhere in your template files for execution. If you echo an incorrect shortcode, it will show up in your pages as text. Make sure that you have fired the code before you echo the shortcode and that the shortcode name is correct!
Labels:
Execute wordpress shortcodes

Previous Article

Responses
0 Respones to "Execute WordPress Shortcode in Widgets and Template Files"
Post a Comment