Using WordPress ‘display_header()’ PHP function

The display_header() WordPress PHP function is used to display the installation header.

Usage

To use the display_header() function, you can pass a string of body classes as an optional parameter. Here is a custom example:

display_header('custom-body-class');

In this case, ‘custom-body-class’ will be added to the body classes of the installation header.

Parameters

  • $body_classes (string, Optional, Default: ”): This is the CSS class or classes that you want to add to the body of the installation header.

More Information

See WordPress Developer Resources: display_header()

This function was introduced in WordPress 3.0. There is no depreciation information as of now. For the source code, you can find it in the wp-admin/includes/template.php file. Related functions include display_setup_form(), display_footer(), and display_sidebar().

Examples

Displaying Header with No Extra Class

In this scenario, we’re simply displaying the installation header with no additional body classes.

display_header();

Adding a Single Class to the Header

Here, we’re adding a single class ‘my-custom-class’ to the installation header.

display_header('my-custom-class');

Adding Multiple Classes to the Header

In this example, we’re adding multiple classes ‘class-1’, ‘class-2’, ‘class-3’ to the installation header.

display_header('class-1 class-2 class-3');

Conditionally Adding a Class to the Header

In this scenario, we’re conditionally adding a class ‘night-mode’ to the installation header if a condition is met.

$night_mode = is_night_mode_active();

if($night_mode) {
  display_header('night-mode');
} else {
  display_header();
}

Adding a Class Based on User Role to the Header

Here, we’re adding a class to the installation header based on the user’s role.

$user_role = get_user_role(); //assume this function returns the role of the user
display_header($user_role);