Using WordPress ‘register_sidebar’ PHP action

The register_sidebar WordPress PHP action fires once a sidebar has been registered. It is used to customize the behavior of the registered sidebar.

Usage

add_action('register_sidebar', 'my_custom_sidebar_function');
function my_custom_sidebar_function($sidebar) {
  // your custom code here
  return $sidebar;
}

Parameters

  • $sidebar: array – Parsed arguments for the registered sidebar.

More information

See WordPress Developer Resources: register_sidebar

Examples

Change the default title tag for widgets

add_action('register_sidebar', 'change_widget_title_tag');
function change_widget_title_tag($sidebar) {
  $sidebar['before_title'] = '<h3 class="widget-title">';
  $sidebar['after_title'] = '</h3>';
  return $sidebar;
}

Add a custom class to sidebar container

add_action('register_sidebar', 'add_sidebar_custom_class');
function add_sidebar_custom_class($sidebar) {
  $sidebar['before_widget'] = '<div class="custom-class">';
  $sidebar['after_widget'] = '</div>';
  return $sidebar;
}

Add an icon before the widget title

add_action('register_sidebar', 'add_icon_before_widget_title');
function add_icon_before_widget_title($sidebar) {
  $sidebar['before_title'] = '<h2 class="widget-title"><i class="fas fa-star"></i> ';
  $sidebar['after_title'] = '</h2>';
  return $sidebar;
}

Modify the sidebar description

add_action('register_sidebar', 'modify_sidebar_description');
function modify_sidebar_description($sidebar) {
  $sidebar['description'] = 'This is my custom sidebar description.';
  return $sidebar;
}

Change the ID attribute of the sidebar container

add_action('register_sidebar', 'change_sidebar_id_attribute');
function change_sidebar_id_attribute($sidebar) {
  $sidebar['before_widget'] = '<div id="custom-id">';
  $sidebar['after_widget'] = '</div>';
  return $sidebar;
}