• Resolved rguttersohn

    (@rguttersohn)


    I am attempting to debug an issue I am having with a translation service API. Currently, I have a filter hook that translates content after it’s gone through the autop filter into a target language.

    It looks something like this currently:

    add_filter('the_content', function($content){
             
         if(in_the_loop()){
                $deepl_api_key = $_SERVER['DEEPL_API_KEY'];
                $translator = new    \DeepL\Translator($deepl_api_key);
                $content_translated = $translator->translateText($content, 'EN', 'ES', ['tag_handling' => 'html'] );
                return $content_translated;
            }
            return $content;
      }, 99);

    It’s working great especially on our blogs. However, I have a content block that makes a WP_Query for posts in a featured content section. Whenever this block is included in the the content, the translation services API throws an error saying the argument must be a string.

    As you can see, I gave this hook a priority of 99, which to me would mean the content at this point would be nothing but HTML. In fact, when I print $content from within this hook, it renders the content as it’s suppose to.

    My questions are:

    Am I correct in assuming the content is pure HTML at the point that this filter is called?

    To debug, is there a final step I can hook into where not only the content but headers, nav and footer have been compiled to an html file that I can then run through this translation service?

    I realize this could be a bug with the translation API or even the custom theme, but I am trying out all of my theories.

    The site is running WP v5.4 and a custom theme built on Sage 9x

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    It used to be the case that server output was the final HTML, but with the introduction of blocks that is no longer the case. Some content is possibly generated client side and would not get translated through a “the_content” hook.

    However, whatever element represents the block server side is still a string. There may not be anything to translate, it may consist entirely of HTML comments at this point. It shouldn’t be throwing a not a string error though.

    It’s possible some custom block does its thing through “the_content”, but is hooked even later than 99. You might try using PHP_INT_MAX instead so your callback executes as late as possible.

    If you still have trouble with the translation API, try adding
    error_log( is_string( $content ) ? '$content really is a string':'Uh-oh, $content is not a string!');
    to your callback so you can verify from your error log that you are really sending a string value to the API.

    Thread Starter rguttersohn

    (@rguttersohn)

    Finally figured it out. I var_dumped str_len($content), and I realized the return was printing next to each item in the problematic block and then printing once for the rest of the content on the page.

    So I added a static variable, set it to 0, var_dumped it and increased it by 1 at the end of the filter’s callback function. It was printing incrementally next to each element in the block.

    This led me to realize that wp was looping through the content starting with this block, which starts out empty.

    Anyhow I added an if statement around the translation block, and it works fine now.

    add_filter('the_content', function($content){
    
            if(strlen($content) != 0){
                $deepl_api_key = $_SERVER['DEEPL_API_KEY'];
                $translator = new \DeepL\Translator($deepl_api_key);
                $content_translated = $translator->translateText($content, 'EN', 'ES', ['tag_handling' => 'html'] );
                return $content_translated;
            }
            
      }, 99);
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do I hook into content after it is completely compiled’ is closed to new replies.