Sidebar blocks

Sidebar blocks show concise information on every page in the columns usually located to the left and right of the content. They can contain everything from static HTML to forms to full-out AJAX applets. Sidebar blocks are added through the method $template->sidebar_widget() which should be called from the hook compile_template.

The $template->sidebar_widget() method

void sidebar_widget(string $title, string $html, bool $use_alternate_class = false)

The title here is what the administrator sees, not what users see. Calling sidebar_widget() does not actually display the block in the sidebar - it only adds the block to the list of registered blocks, after which the administrator must go into the sidebar editor and add the block.

The HTML should just be the HTML that goes inside the block.

There are typically two CSS classes used for blocks: one for blocks that are lists of links, and one for blocks that contain arbitrary HTML. Plugin sidebar blocks default to the latter, for good reason: some themes might make all your links block-level or otherwise mess up your layout inside the block. The $use_alternate_class parameter allows you to use the former type of block. Most developers will not need to use this option.

Example

$plugins->attachHook('compile_template', 'myplugin_add_sidebar();');
 
function myplugin_add_sidebar()
{
  global $template;
  $my_html = '<p>Hello, world!</p>';
  $template->sidebar_widget('My plugin', $my_html);
}

Categories: (Uncategorized)