Replace SEO title tag with dynamically generated value
-
I have a page that pulls data from amazon product api and return the data for a category, baseed on the category ID.
The script runs in a hook whitch outputs the data in a page from a custom post type (labeled amazon) I created for the purpose
Everything works as expected except that I want the Name of that category to replace the <title> tag of that post type.
Here is where I am stuck
In the script that calls the amazon. api, at some point, I retrieve the value of the category title:
$newtitle = $Category_Title_From_Json_Response;
Then I store the value in a global variable so it can be accessed from other scripts. (suggestions from chatGPT)
global $custom_document_title;
$custom_document_title = $newtitle;
Then In my functions.php file, still suggested by chatGPT, I have this code:add_filter( 'pre_get_document_title', function( $title ) { // Access the global variable containing the dynamic title. global $custom_document_title; if ( is_singular( 'amazon' ) && isset( $custom_document_title ) ) { // $newtitle = "My custom title"; // This works $newtitle = $custom_document_title; // This does not pick the value and returns NULL :-( } else { // Otherwise, use the default title. $newtitle = $title; } return $newtitle; }, 999, 1 );
So, how can I pass the value of $newtitle from the hook to the filter?
I hope this makes sense ??
Thank You already
- This topic was modified 1 year, 7 months ago by .
- This topic was modified 1 year, 7 months ago by .
- This topic was modified 1 year, 7 months ago by .
The page I need help with: [log in to see the link]
- The topic ‘Replace SEO title tag with dynamically generated value’ is closed to new replies.