How to Add a Shortcode in WordPress
Ever wanted to add some specialized content to your websites post or page and at the same time you don’t want to have heavy footprint of any third party plugin? Well WordPress has support for that too! Heard of shortcodes? You can consider shortcodes as inbuilt plugin that has been provided to you by WordPress itself. You don’t have to do any fancy configurations to leverage the functionality, you need to just put some basic pre-defined text in your page or post and your are done.
We will cover some basics of this terminology in this article. We will take you through process of developing and using shortcode in wordpress.
What is a WordPress Shortcode?
As said earlier, shortcodes are nothing but small piece of code that will help you to add some specialized content to your page or post. It helps you to cut down on repetitive strings of HTML, and you can use it anywhere in your site. e.g. you can use shortcode to insert some unique buttons in your post, you can add some data columns to your page for better presentation etc.
We will shortly see how its defined and how the code is interpreted.
How do I Create A Shortcode?
Shortcode creation is as easy as eating a cake (free). Here we will create a shortcode that will insert one link button in our post or page. We will add some css to our main styles css and that will be used when shortcode is rendered. Our shortcode will render following html
1 |
<a href="https://www.opencodez.com" target="_blank" class="my_button">this is shortcode link button</a> |
If user has to add this code manually all the time, he has to remember the class name to add for button. Its not feasible if page or post has many such scenarios. So instead of adding above html we will create a short code that will be easy on usage. To add shortcode we will simply add below code to our themes functions.php
1 2 3 4 5 6 7 8 9 |
function my_button( $atts, $content = null ) { extract(shortcode_atts(array( 'url' => '#', 'target' => '_self' ), $atts)); $out = "<a href=\"" .$url. "\" target=\"" .$target. "\" class=\"my_button\">".do_shortcode($content)."</a>"; return $out; } add_shortcode('button', 'my_button'); |
Above code is adding one shortocde “button” to our wordpress system and binding it to function “my_button”. This function will take parameters from your shortcode and print them out the way we want.
How do I Use my Shortcode in WordPress?
Now we have added shortcode to our theme, its time we use it. Usage is easier than adding it. To use above defined shortcode and insert a button in your post add below text in your page or post-
1 |
[button url="http://test-shortcode.com" target="_blank"]this is shortcode link button[/button] |
You can see that we are sending link value as an attribute to above shortcode. This url will be extracted in the function we have defined above and assigned in actual html that is going to render. You will see button like below
Dont forget to add some css to your styles.css that will give your button above look. You can copy css from here-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
.my_button { display: inline-block; padding: 8px 12px; margin-bottom: 0; font-size: 14px; font-weight: normal; line-height: 1.428571429; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; border: 1px solid transparent; background-color:#111; color:#f00; font-weight:bold; border-radius: 4px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none; } |
Thats it!! you are done, you have your own shortcode that can do any magic for you.