Archive

Posts Tagged ‘wordpress hooks’

Add menu into wordpress page

May 11, 2011 Leave a comment

Hello Folks,

Since many days i was wondering to add a dynamic menus that i could insert via plugin. I wanted to insert login and logout buttons into menu bar without editing the template. Then i found out one hook for the wordpress that i could inject my content into menu.

      add_filter('wp_nav_menu_items',add_menu,10,2);

The above hook will call add_menu which contain 2 parameters.

      function add_menu($items,$args)
      {
            items .= "<li>Another Menu</li>";
            return items;
      }

it will look as following


As you can see another menu is created beside. As css is not applied it looks like that apply css would
look alike.

If there is any problem give a reply.

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: