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.
2
Answers
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
Also don’t forget to check and sanitize your
$_GET['id']
if needed.You’re looking for get_query_var()
One small thing to understand is that
get_query_var()
only retrieves public query variables that are recognized byWP_Query
.So we need to register our variable first. This is considered best practice. Simply using
$_GET['id']
is considered to be unsafe within the WordPress ecosystem.Also you should use something other than
id
as it is already used by WordPress to handle theWP_Query
. I’ve usedref
(reference) but you can choose whatever as long as it doesn’t impact WordPress core.then we can simply built our custom title.
Note that, if your theme is using WordPress to set your pages title, you might need to set some sort of conditional statement around
add_theme_support( 'title-tag' );
, in yourfunction.php
.Something like…
The
title-tag
is usually included in your theme options.