There is an action get_header
that you can use. In your theme’s functions.php
, register a callback for that action:
add_action( 'get_header', function( $name ) {
add_filter( 'current_header', function() use ( $name ) {
// always return the same type, unlike WP
return (string) $name;
});
});
You could also write a little helper class that you can reuse:
class Template_Data {
private $name;
public function __construct( $name ) {
$this->name = (string) $name;
}
public function name() {
return $this->name;
}
}
add_action( 'get_header', function( $name ) {
add_filter( 'current_header', [ new Template_Data( $name ), 'name' ] );
});
In your header.php
, you get the current part/name with:
$current_part = apply_filters( 'current_header', '' );
You can do the same with get_footer
, get_sidebar
and get_template_part_{$slug}
.