I have an page (with snippet code and short code and manual php code) and it call a REST API and base Of response, I want change tag title of this page (Not in database).and it was change every call.
i try do :
in snippet code:
$GLOBALS['MyTitle'] = $response-> title;
in function:
function change_title($title) {
$variable = $GLOBALS['MyTitle'];
if (!empty($variable))
{
$title = $variable;
}
return $title;
}
add_filter('pre_get_document_title', 'change_title',500);
but it was empty every time because the function section run sooner then snippet code(Api Call).
WordPress has not any function for change title tag text? like wp_set_title('my title')
to use without add_filter
?
2
Answers
Changing title of the page dynamically by applying the filter the hook used for the filter is document_title_parts. Here is the official wordpress documentation link and below is the example. In this example i have used get method for API.
Screenshot for your reference
In WordPress, we have an action to change the title programmatically.