Eric Levy
Eric Levy

Reputation: 11

Modifying header meta data from page template

I am calling a custom page template with the following code:

add_filter( 'template_include', function( $template ) {

    return get_stylesheet_directory() . '/tt_location_template.php';
} );

I have a database lookup within tt_location_template.php that is to a custom database and delivering custom page information depending upon the page (so we don't have to have hundred/thousands of pages built out in WP, since it is all custom data being delivered.) I would like to be able to modify the title and meta tags in the header from tt_location_template.php. Is this possible with the following code or does this code need to be called from functions.php? Is there another way to do it? I would prefer to keep all of my database queries in tt_location_template.php and not have to migrate them to functions.php.

function theme_xyz_header_metadata() {
    
    // Post object if needed
    // global $post;
    
    // Page conditional if needed
    // if( is_page() ){}
    
  ?>
    
    <meta name="abc" content="xyz" />
    
  <?php
    
}
add_action( 'wp_head', 'theme_xyz_header_metadata' );

Looking for best practices.

Upvotes: 0

Views: 55

Answers (0)

Related Questions