Creating a self-closing shortcode
The simplest shortcode is the self-closing one. We’re going to create a simple link to our Twitter account, and then add it in a blog post.
All the code goes in functions.php, which is located in /wp-content/themes/your-theme/. If you don’t have one, just create it and put the code in it.
<?php function button_shortcode() { return 'Follow me on Twitter!"'; } add_shortcode('button', 'button_shortcode'); ?>
How to Usage:
[button]
Simply using the the add_shortcode()
function, we can link any php function to our shortcode. In this simple example, all we do is returning a link to our Twitter account, but let’s take this a step further and add some parameters.