Inserting Widgets With Shortcodes

Learn how you can insert widgets in your WordPress

The shortcode ability of WordPress is extremely underrated. It enables the end user to create intricate elements with a few keystrokes while also modularizing editing tasks. In a new theme, Daniel Pataki decided to look into adding widgets anywhere with shortcodes, and it turns out that it isn’t that difficult. He will be looking at the widgets object and shortcodes without delving into too much detail about how and why they work.

 

The first thing Daniel Pataki looked into was how to output any widget without shortcodes. Once done, implementing a shortcode is a relatively trivial matter. Digging around in the Codex, he found the the_widget() function, which does just what he wants. It takes three parameters: the widget’s class name; the widget’s instance settings; the widget’s sidebar arguments. Once Daniel Pataki saw this, his face lit up. Not only can hr output a widget anywhere, but he can pass different sidebar arguments to any widget. This is great because he can specify parameters such as before_widget and after_widget. This also opens up the possibility of easily changing the style of the widget from within the shortcode.

Comments

Be the first to write a comment

You must me logged in to write a comment.