Archive

Posts Tagged ‘programatically’

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.

Advertisements

Adding template page from plugin

April 18, 2011 Leave a comment

Hello readers,

I have found a very simple solutions to assign a template page to specific or all pages . According to my previous tutorial for creating the template page and placing into the wordpress current template.

For creating the template you need to add comment on the top of the page as known from my blog

/*
Template Name: new template
*/

Now what ever you want add the functionality to the page and then add header/footer as on your preference.
Now your template file is ready to run or attach to page. But now no need to copy that php file to the template folder of wordpress.
all you need to do is call the following hook

add_filter( 'page_template', 'catch_plugin_template' );

Now this will be called on every page/post load.

add_filter( 'page_template', 'catch_plugin_template' );
function catch_plugin_template($template)
{
if(is_page())
{
$template = WP_PLUGIN_DIR . '/yourpluginname/template-file.php';
return $template;
}
}

Now the above could will execute on every page and the template would be assigned to all the pages you have created.

Thank You for reading.

%d bloggers like this: