Using WordPress ‘get_allowed_block_template_part_areas()’ PHP function

The get_allowed_block_template_part_areas() WordPress PHP function returns a filtered list of allowed area values for template parts.

Usage

$allowed_areas = get_allowed_block_template_part_areas();

Parameters

  • None

More information

See WordPress Developer Resources: get_allowed_block_template_part_areas

Examples

Display a list of allowed template part areas

This example retrieves the allowed template part areas and displays them as an unordered list.

$allowed_areas = get_allowed_block_template_part_areas();

echo '<ul>';
foreach ($allowed_areas as $area) {
    echo '<li>' . $area . '</li>';
}
echo '</ul>';

Check if a specific area is allowed

This example checks if ‘header’ is an allowed template part area.

$allowed_areas = get_allowed_block_template_part_areas();

if (in_array('header', $allowed_areas)) {
    echo '**Header** is an allowed template part area.';
} else {
    echo '**Header** is not an allowed template part area.';
}

Register a new allowed area

This example registers a new allowed area called ‘custom’ using the allowed_block_template_part_areas filter.

add_filter('allowed_block_template_part_areas', 'my_custom_allowed_area');

function my_custom_allowed_area($allowed_areas) {
    $allowed_areas[] = 'custom';
    return $allowed_areas;
}

Remove an allowed area

This example removes the ‘sidebar’ allowed area using the allowed_block_template_part_areas filter.

add_filter('allowed_block_template_part_areas', 'remove_sidebar_allowed_area');

function remove_sidebar_allowed_area($allowed_areas) {
    $key = array_search('sidebar', $allowed_areas);
    if ($key !== false) {
        unset($allowed_areas[$key]);
    }
    return $allowed_areas;
}

Count the number of allowed areas

This example counts the number of allowed template part areas and displays the result.

$allowed_areas = get_allowed_block_template_part_areas();
$area_count = count($allowed_areas);

echo 'There are **' . $area_count . '** allowed template part areas.';