You probably have faced the situation where you want to insert some special function of your own choice in your article. If you are unable to find a plugin fulfilling your requirement, you need to write your own snippet. But this is not easy. We are bloggers, not the coders.
Writing a plugin in php is not an easy task and one have to get familiar with basics at least. So how to get our custom made feature in WordPress.
I faced this situation while inserting Google adsense in middle of my posts. I was supposed to rely on plugin and to manually insert the code in my posts. Matter is not ended here. I also need to customize the output of plugin. All of this became very hectic. Then after Googling on net, I found a solution and am very happy with it.
WordPress supports shortcode. What is short code?
Short code enables you to make you custom made function or event, triggered by easy to remember text line. For example, I created a shortcode to insert the adsense within my post which I call with help of simple syntax “adsense” , word adsense is with in braces. By inserting the code , I am able to insert Google adsense within my post, pre-formatted as per my choice.
[adsense]How did I do that?Lets understand the basic code of short code.
function showads() {
return 'your code here
';
}
add_shortcode('adsense', 'showads');
We have following benefits of using shortcode functionality;
-
Time saving activity,
-
Custom Made Event,
-
Ability to design Custom Functionality,
-
Prevents from coping and pasting long codes,
-
No need to install plugin,
-
Database is not modified and have compact size.
We declared a function called showads, and instructed WordPress to add it as short code which is called with the help of text line “adsense”. So my shortcode command to insert the adsense is “[google_ads]”.
You can create your own function by simply putting the code with in the code given above. Eight different uses of shortcode have been demonstrated in an article. Visit it , understand the code and have fun.
{ 1 comment… read it below or add one }
Great tutorial. I personally prefer codes instead of plugins.
.-= Tech Maish´s last blog ..Useful Ipod Tips and Trick =-.
{ 11 trackbacks }