I've got a page called /team-page/?id=x that's built in WordPress
The URL parameter of "id" determines the content that will dynamically show on that page. However, my meta title for that page is statically set. Is there a way I can dynamically set the meta title based on the page content? Each variation of /team-page will have a unique H1 - ideally, I'd love to just grab this H1 and set it as the meta title.
You can achieve it with document_title_parts
filter. It takes $titles
as parameter, which consist of two parts - title and site (except for front page - instead of site it's tagline)
So try this
add_filter( 'document_title_parts', 'custom_title' );
function custom_title( $title ) {
// Just to see what is $title
// echo '<pre>';
// print_r( $title );
// echo '</pre>';
// die();
$uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); // get current uri without query params
// Change meta only on team-page.
if ( '/team-page/' === $uri ) {
// you can do here anything you want
$title['title'] = get_the_title() . ' and whatever string you want ' . $_GET['id'];
}
return $title;
}
Also don't forget to check and sanitize your $_GET['id']
if needed.