Using WordPress ‘header_textcolor()’ PHP function

The header_textcolor() WordPress PHP function displays the custom header text color in 3- or 6-digit hexadecimal form, without the hash symbol.

Usage

echo header_textcolor();

If the custom header text color is “#FF5733”, the output will be “FF5733”.

Parameters

  • None

More information

See WordPress Developer Resources: header_textcolor()

Examples

Display header text color in an inline style

Display the custom header text color in an inline CSS style for an HTML element.

// Display header text color in an inline style for an h1 element
echo '<h1 style="color: #' . header_textcolor() . ';">Hello, world!</h1>';

Add header text color to a CSS class

Create a CSS class with the custom header text color as the text color.

// Add header text color to a CSS class in a style block
echo '<style>.header-color { color: #' . header_textcolor() . '; }</style>';

Conditional output based on header text color

Output different messages based on the custom header text color.

// Get header text color
$header_color = header_textcolor();

// Display different messages based on header text color
if ($header_color == '000000') {
    echo 'The header text color is black.';
} else {
    echo 'The header text color is not black.';
}

Change header text color based on a condition

Set the custom header text color based on a condition, such as the user role.

// Get current user role
$current_user = wp_get_current_user();
$user_role = $current_user->roles[0];

// Set header text color based on user role
if ($user_role == 'administrator') {
    set_header_textcolor('FF0000');
} else {
    set_header_textcolor('000000');
}

Check if the header text color is a valid hexadecimal value

Check if the custom header text color is a valid 3- or 6-digit hexadecimal value.

// Get header text color
$header_color = header_textcolor();

// Check if the header text color is a valid hexadecimal value
if (preg_match('/^([A-Fa-f0-9]{3}){1,2}$/', $header_color)) {
    echo 'The header text color is a valid hexadecimal value.';
} else {
    echo 'The header text color is not a valid hexadecimal value.';
}