Home > Wordpress Development > Shortcode with wordpress

Shortcode with wordpress

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

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: