• 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 Giu.
    • This topic was modified 1 year, 7 months ago by Giu.
    • This topic was modified 1 year, 7 months ago by Giu.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • You’re using the Yoast SEO plugin for these “SEO titles” (among other things), so you should be using their wpseo_title filter. Here’s their documentation: https://developer.yoast.com/features/seo-tags/titles/api/

    You’re also using the PREMIUM (ie paid) version of the Yoast SEO plugin. So if you need further help with this, you may want to open a ticket in their helpdesk so their support staff can help you.

    Good luck!

Viewing 1 replies (of 1 total)
  • The topic ‘Replace SEO title tag with dynamically generated value’ is closed to new replies.