Shortcodes in WordPress are bits of text you can use in the content area to invoke some kind of function to accomplish certain tasks. For example, video embedding in WP 2. You can write your own shortcodes, and plugins often offer their functionality via shortcodes as well. You can invoke it with a special function:.
Understanding what custom shortcodes are
Konstantinos Kouratoras is a software engineer based in Crete, Greece. WordPress lover and amateur photographer. When a shortcode is inserted in a WordPress post or page, it is replaced with some other content. In other words, we instruct WordPress to find the macro that is in square brackets  and replace it with the appropriate dynamic content, which is produced by a PHP function. The usage is pretty simple. We could use something like this:. For a more advanced shortcode, we could set the number of posts to display by setting a parameter:.
Allowing the Use of Shortcodes in the Text Widget
However, by default, WordPress only allows the use of shortcodes in posts and pages , and not anywhere else. WordPress provides several widgets by default. You can also use it to add any HTML code. It accepts one required parameter, the text to parse, and it returns the parsed text. The code below can be used in a plugin file or in the functions. Alternatively, we can also create our own widget. The first thing to change is the name of the class. As the constructor of this class sets some information about the widget itself, we also need to modify it.
Work fast with our official CLI. Learn more. If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again. There was a problem preparing your codespace, please try again.