Creating Shortcodes in WordPress

Shortcodes enable developers to create special kinds of content, following the idea of macros. One very famous short code is “gallery” needed to insert a gallery of images into a post.

The good thing about shortcode’s is that final users are free to decide where to insert them.

WordPress’s Shortcode API handles all the tricky parsing, making developers life much easier. The following is a snippet for a Shortcode – in order to make it work you have to add it to your functions.php file in your theme’s directory.

function get_custom_text($atts) {
    return 'This is my <strong>custom</strong> text';
}

add_shortcode('my_text', 'get_custom_text');

 

To make it work you have to make the reference from any post like this:

[my_text]

 

You can get all the details from the WordPress Codex API webpage.

Enjoy.

Leave a Reply