Archive

Posts Tagged ‘hooks’

Shortcode with wordpress

February 16, 2011 Leave a comment

Hello readers here is one more article for creating a shortcode into the wordpress . Like if you want to have custom tags to be written into a post and should replace it with another function then could be achieved it in minutes ..
Say for example you want to add a tag [google] which will replace with http://www.google.com,so here we could do.
First decide what shortcode you want to name as for now we have named it [google].
Step 1:
Not time for calling the wordpress function. Yes wordpress has provided inbuilt function creating shortcode.


add_shortcode(name-of-shortcode,function-name);

This above function would call the specified function whenever wp will find the shortcode. In our case [google].


add_shortcode('google',get_google_url);

So now the get_google_url would echo the http://www.google.com url.


add_shortcode('google',get_google_url);

function get_google_url()
{
      return "http://www.google.com";
}

Oh thats done !! basic of the shortcode is done .. Thats easy now will go ahead to get parameters from the shortcode. Like if we want to add search url like http://www.google.com?q=search+value So with shortcode [google supercar] will give output like http://www.google.com?q=supercar . Could achieve by the following way

add_shortcode('google',get_google_url);

function get_google_url($atts)
{
      return "http://www.google.com/search?q=".$atts[0];
}

So now where ever you will put [google value] then it will output google search url.

but remember if there are two parameters like [google value1 value2] they would be treated as seperate parameters not one .

Hope its easy to learn wordpress .

Thanx Readers.
Ketan Mujumdar

%d bloggers like this: