There’s no such filter, so what you’re doing is about the best you can do. However, you can optimise a bit by creating functions that wrap the template functions while applying your filter:
add_filter(
'filter_template_part_args',
function( $args, $slug, $name = null ) {
$args = wp_parse_args(
$args,
[
'id' => esc_attr( basename( $slug ) ),
'tag' => 'div',
'class' => '',
'container' => 'container',
'row' => 'row',
]
);
return $args;
}
)
function mytheme_get_template_part( $slug, $name = null, $args = [] ) {
$args = apply_filters( 'filter_template_part_args', $args, $slug, $name );
get_template_part( $slug, $name, $args );
}
function mytheme_get_header( $name = null, $args = [] ) {
$args = apply_filters( 'filter_template_part_args', $args, 'header', $name );
get_header( $name, $args );
}
Then you can just do the same with get_sidebar()
and get_footer()
, and you should be covered for all the main template functions:
function mytheme_get_footer( $name = null, $args = [] ) {
$args = apply_filters( 'filter_template_part_args', $args, 'footer', $name );
get_footer( $name, $args );
}
function mytheme_get_sidebar( $name = null, $args = [] ) {
$args = apply_filters( 'filter_template_part_args', $args, 'sidebar', $name );
get_sidebar( $name, $args );
}