WordPress logo

WordPress Shortcodes

Shortcodes are used to insert predetermined functions or HTML codes inside your WordPress post or pages. This article will explain how you create shortcodes and how you can use it to minimize the website build time.

Create wordpress shortcodes

Go to your functions.php file in your theme. Shortcodes need to initialize from add_shortcode('shortcodename','function name to execute') function.

Let’s write a simple shortcode example to show strong, red colord text on your post

First, create a function to accept text

function coloredtext($atts, $content = null){
    $a = shortcode_atts( array(
        'color' => '000',
        'size' => '12',
    ), $atts );

    return '<span style="color:#'.$color.';font-size:'.$size.'px;">' . $content . '</span>';
}

add_shortcode('textcolor', 'coloredtext');

WordPress shortcodes are better for page-wise HTML formatting.

To learn more about shortcodes click here

About the author