How to Create Custom Shortcode for WordPress

WordPress shortcodes introduced in its early version 2.5 before about six years ago and since that time, shortcodes are used to ...




WordPress shortcodes introduced in its early version 2.5 before about six years ago and since that time, shortcodes are used to increase the functionality of WordPress.
The shortcodes are a particular tag that you can enter into a post or a page which gets replaced with different content when visitors are viewing the post or page on the website.
You’ve probably used shortcodes before, but did you know it’s pretty easy to create your own, with no plugins necessary?
In this tutorial, I will show you how to build some simple WordPress shortcodes for the subscription that will help you to create any functionality in future as you like.
You can directly create your shortcode function in the functions.php file or build a simple plugin. I'll show you both but at first, I go through the functions.php file.

Simple Facebook Follow ShortCode

In the beginner part of this tutorial, I will create a simple shortcode that will do the similar task in every time whenever you type it in the editor.
First, you need to create the callback function that will return the message what you want to show (in shortcodes we don’t echo anything, everything returned).
Just go to your functions.php file and paste the code snippet at bottommost.
function subscribe_link(){
    return 'Follow us on <a rel="nofollow" href="https://www.facebook.com/RAIMU/">Facebook</a>';
}
add_shortcode('subscribe', 'subscribe_link');

Test the ShortCode

Our shortcode is ready to use. Just add the code [subscribe] to your post or page wherever you want to show it.
See the image in below; it works correctly.

How To Add Parameters in Shortcode

Usually, you need to add a dynamic functionality in your shortcode and don't want to show the same content in every time.
As I am talking about subscription, there are lots of social networking sites in today's world; you may also want to get touched with your audience through many social sites. Therefore, I will show you how to add a parameter to the shortcode to change URL of social sites.
Again, just go to your functions.php file and add the following codes in there.
function subscribe_link_att($atts) {
    $default = array(
        'link' => '#',
    );
    $a = shortcode_atts($default, $atts);
    return 'Follow us on '.$a['link'];
}
add_shortcode('subscribe', 'subscribe_link_att');

Test ShortCode Parameteres

Now insert the following code to your post or page wherever you want to show it.
[subscribe link='https://www.facebook.com/ThemeXpert/']
[subscribe link='https://www.twitter.com/ThemeXpert/']
So, here is the image that shows it works correctly. 

Working with Content

You have noticed that the previous example shows the direct link to social sites, but this process is not user-friendly. It looks good if it is possible to bind the links in HTML anchor tag.
Therefore, I will now show you how to working with contents within a shortcode.
It is quite same with the previous example just a tiny differences.
You need to add one extra parameter in the function and initially declare its value as null. And then call the do_shortcode function to use the contents.
See the following code.
function subscribe_link_att($atts, $content = null) {
    $default = array(
        'link' => '#',
    );
    $a = shortcode_atts($default, $atts);
    $content = do_shortcode($content);

    return 'Follow us on <a href="/.$a['link']." style="color: red">'.$content.'</a>';
}
add_shortcode('subscribe', 'subscribe_link_att');

Testing Content ShortCode

Insert the following code to your post or page.
[subscribe link='https://www.facebook.com/USERNAME/']Facebook[/subscribe]
[subscribe link='https://www.twitter.com/USERNAME/']Twitter[/subscribe]
Look at the image. I have add style in the anchor tag. Hence, the link text looks red. 

ShortCode with a Plugin

If you don't want to customize your functions.php file, you can create a simple plugin to utilize this shortcode functionality.
To create a plugin just go to your plugin folder and make a folder named subscriptions and create a PHP file inside it with the same name, that means the plugin folder name and PHP file name will be same.
Now add the heading details of the WordPress plugin. You may follow the example in below:
/*
Plugin Name: Subscriptions
Plugin URI: https://example.com
Description: A very basic test plugin for subscriptions
Version: 1.0
Author: RAIMU
Author URI: https://example.com
License: GPL2
*/
Cut the code from your functions.php file which you added a few minutes ago and now insert here in the plugin file, below the heading information of plugins file.
Now you can see your plugin in the plugins directory. Just activate the plugin and follow the same process as before. 
I hope you have enjoyed this whole tutorial on creating a unique shortcode for your WordPress website. Share your opinion through the comment section.
Name

About US,1,activate,1,ADDMEFAST,1,AdLinkFly,1,Adobe Illustrator,1,Adsense,17,ANALISA WEB,1,android,39,anonymous-surfing,1,Aplikasi,40,Artikel,11,Backlink,7,Backlinks,20,Backup,1,BBM,5,beginners,1,Best 140+ Do-follow Web 2.0,1,Best 59+ Free Ping Submission Sites For Faster Indexing,1,Best High DA Dofollow Blog Commenting,1,BIN,1,Blog,27,Blog Design,2,Blogger,1,Blogspot,3,Bounce Rate,1,Canva pro,1,CDN,1,Chrome,1,Copy Artikel,1,cPanel,1,crack,2,darknet,1,darkweb,1,Desain,1,DevTools,1,DIGITAL MARKETING,1,Disposable E-mail Free,1,Disqus,1,domain,1,download,2,DropMyLink,1,Dunia Kampus,1,e-books,1,EDU BACKLINKS,5,Envato Elements,1,Express Vpn Premium Account,1,FAQ SCHEME PAGE,1,Followers,1,Footer,1,free,1,Free Courses,2,Free Credits,1,Free Image Submission Sites,1,free methods,1,Free Profile Creations Sites List 2020-2021,1,Free RDP,1,Free Video Submission Sites List 2020-2021,1,freelancing,1,Gadget,7,giveaway,1,Gmail,2,Google,3,GOOGLE ADS,1,Google Adsense,2,GOOGLE ANALYTICS,1,Google Dork,1,google drive,2,Google My Business Customer Reviews,1,GOOGLE SEARCH ENGINE,1,GoogleNews,1,Grammarly Premium Cookies,1,guide,2,Guides,1,Header,3,High Authority Blog Submission Sites List,1,High DA/PA PPT Submission Sites List 2020-2021 for SEO,1,hosting,1,Hotstar Premium,1,How To,1,How to Activate,3,HTML,5,IDM,1,Info Blog,11,Info Google,13,Inspirasi,5,Instagram,4,internet,1,Jejaring Sosial,12,JNews,1,JSON-LD,1,Kapita Selekta,3,Komentar,8,Komputer,11,Kurikulum 2013,2,Label,3,learning,1,LINE,3,Link,14,LINK BUILDING,1,Lynda,1,Lynda.com,1,Majalah Wanita Online,1,MENINGKATKAN SEO WORDPRESS,1,Menu,2,MICRODATA SEO,1,MultCloud,1,multi cloud management,1,MyThemeShop,1,Netflix Accounts,1,News,38,newsmag,1,newspaper,1,Noor theme,1,Nord Vpn Premium Account In April 2020,1,NORD VPN PREMIUM ACCOUNTS,1,On Page SEO,8,online shop,1,Opini,10,OVHcloud,1,Page Speed,1,Pagerank,8,PDF Submission,3,PDF Submission Sites,1,Pembelajaran,4,Pixelyoursite,1,plugin,1,plugins,2,post views,1,Posting,10,Premium cookies,1,Privacy Policy,1,Produk,1,proxy,1,PVANISH PREMIUM ACCOUNT,1,reset-password,1,Review,52,RSS,1,RSS Feed Submission Sites List 2020-2021,1,Schema,1,Search Engine,4,SEARCH QUALITY RATERS,1,Secret Hacks,1,SEF,8,seo,98,SEO Tools,3,SEO WORDPRESS,1,Settings,1,Shopify,1,Shopping,1,Shortcode,1,Sidebar,2,social media,14,SoundCloud,1,Spam,1,Spotify,1,Spotify Premium Accounts,1,STM Forum,1,Submission,1,tagdiv,1,Template,9,themes,2,Tips,3,Tips & Tricks,17,TIPS DAN TRIK,2,tips-tricks,4,Tokoh,1,Tools,10,Tricks,9,Trik,1,umum,1,VPS,1,Web 2.O Sites List 2020,1,Web Hosting,2,Webmaster Tools,6,Website,3,WhatsApp,16,Widget,10,windows,1,Windows 11,1,wordpress,23,WORDPRESS & WOOCOMMERCE TUTORIALS,1,WP Rocket,1,youtube,18,YouTube 2021,1,
ltr
item
Trik.My.ID - Trik dan Tips Untuk Semuanya: How to Create Custom Shortcode for WordPress
How to Create Custom Shortcode for WordPress
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGTGeD2yrhq-s8T3meCGyB1pmDVpMmJPPzXr25PTkoCknOevP5HErn1Bh_eiWxjHx_ieChjx4SWrfAjeHrHvtzUxhwOFA7AmFBaFxvSklrrA-S2po7SBOjmBALrNmCLQ5TbT0h7Ntx5bEf/s640/b2ap3_large_cover.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGTGeD2yrhq-s8T3meCGyB1pmDVpMmJPPzXr25PTkoCknOevP5HErn1Bh_eiWxjHx_ieChjx4SWrfAjeHrHvtzUxhwOFA7AmFBaFxvSklrrA-S2po7SBOjmBALrNmCLQ5TbT0h7Ntx5bEf/s72-c/b2ap3_large_cover.jpg
Trik.My.ID - Trik dan Tips Untuk Semuanya
https://www.trik.my.id/2020/03/how-to-create-custom-shortcode-for.html
https://www.trik.my.id/
https://www.trik.my.id/
https://www.trik.my.id/2020/03/how-to-create-custom-shortcode-for.html
true
3610684226484108978
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content