How to create a WordPress shortcode?

WordPress Shortcodes allow you to extend the functionality of WordPress. Shortcodes are text within the square bracket. You can paste this shortcode on any page, post or widget. WordPress executes the PHP function associated with the shortcode and displays the result instead of the actual shortcode text. You can find more about WordPress shortcodes at

https://codex.wordpress.org/Shortcode_API

To create a shortcode, open functions.php in your theme folder, add the following code at the end of the file

//[helloworld]
function helloWorld_func( $atts ){
   return "Hello World from shortcode";
}
add_shortcode( 'helloworld', 'helloWorld_func' );

Now your shortcode is ready. You can place the shortcode on any page, post, or widget by placing the following text

[helloworld]

It will display the text Hello World from shortcode.

A shortcode is a normal PHP function, it can do more complex things like connect to MySQL database, fetch data from a remote API, do calculations, etc…

See WordPress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *