1

I'd like to be able to pre-populate all occurances of the get_tempate_part $args when setting up a theme.

get_template_part( string $slug, string $name = null, array *$args* = array() )

Several action hooks exist within the get_template_part function and within locate_template which is also called by get_template_part, get_header, get_footer and the like but no filters are applied. The best I've been able to fashions is to modify every instance of my themes call to get_template_part like so:

<?php get_template_part( 'views/loop', 'index', apply_filters( 'filter_template_part_args', [], 'loop' ) ); ?>

and run a custom filter like so:

add_filter( 'filter_template_part_args', function( $args, $slug, $name = '' ) {

    $defaults = [
        'id'        => esc_attr( basename( $slug ) ),
        'tag'       => 'div',
        'class'     => '',
        'container' => 'container',
        'row'       => 'row',
    ];

    return \wp_parse_args( $args, $defaults );
    ;

} );

the above function work around - whilst being maintenance heavy – works for the individual teamplate_part calls made in the theme, but fails for existing calls from wordpress defaults (get_footer, get_header, etc)

Am I missing some key bit of info? Yes, get_header() will except an $args param – but my theme is rapidly filling up with multiple code chunks that make extensibility painful and very un-DRY

2
  • $args was introduced to give us a way of passing variables to template parts without having to resort to global variables. It looks like you’re totally overworking things when what you want to do is use a global variable. Mar 26, 2021 at 13:15
  • Please don't mistake my misuse of term global to indicate a global Variable. A better term might have been 'system wide'. I too find globals and statics to be a lazy way to pollute a system. I have removed my use of the term from my question
    – dj.cowan
    Mar 26, 2021 at 13:21

1 Answer 1

2

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 );
}
1
  • Will +1 this answer as it does provide a working solution. Doesn't help us with our main goal of harnessing core functionality and thereby minimising the need for custom functions but it will aid us in decoupling our template files from framework dependancies
    – dj.cowan
    Mar 27, 2021 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.