DEV Community

Chris Texe
Chris Texe

Posted on • Originally published at madlon.eu

Shortcode in WordPress

Have you ever wanted to make a shortcode in WordPress? I will show you how to make it without plugin!

First we need to open functions.php file. This file is located in our theme directory. Paste there this code:

Then on any page or post you can display message from this shortcode by writing this:

[my_heading]

The best method in WordPress is using the dedicated type of block: shortcode.

If you want you can use any HTML formatting in your shortcode, eg.:

function my_shortcode() {
    $message = '<h1 style="font-size:60px; font-weight: bold;">My big HEADING</h1>';
    return $message;
}
add_shortcode('my_heading', 'my_shortcode');
Enter fullscreen mode Exit fullscreen mode

As you see in the second line, I used here CSS formatting.

Link to original article Shortcode in WordPress.

Top comments (2)

Collapse
 
tw2113 profile image
Michael Beckwith

on the flipside, if you want this to persist regardless of what theme you're using, it's not difficult at all to make your own quick plugin with almost all the same code (excepting the plugin header), and have the shortcode persist across themes. Plugins can very much be individual single purpose functions.php files that you have an extra step to take in activating.

Worth consideration.

Collapse
 
texe profile image
Chris Texe

You're right! This tip is for beginners and I didn't wanted to make a mess in their head. 😁