• Struggles with invalid referer when you have API key restrictions? You will need to edit the load_content function in yotuwp.php. By default, wp_remote_get does NOT send the Referer in the header information. After digging into #37820 it looks like a simple fix, but has to be done in the code. After editing the area where it says $response = wp_remote_get to add the headers section to the array, this plugin will start working. 
    To debug and test before and after, add error_log(print_r($response, true)); directly after the original $response line.


    public function load_content( $url ) {

            $url .= '&key='. trim($this->api['api_key']);
            if ( $this->is_cache) {
                $cache_id       = md5( $url);
                $cache_content  = $this->cache( $cache_id );

                if( !is_string( $cache_content ) ) {
                    $response = wp_remote_get( $url, array(
                        'timeout' => $this->timeout,
                        'headers' => array(
                            'Referer' => home_url()
                        )
                    ));
                    $this->cache( $cache_id, json_encode( $response ) );
                }else $response = json_decode( $cache_content, true);

            }else
            $response = wp_remote_get( $url, array(
                'timeout' => $this->timeout,
                'headers' => array(
                    'Referer' => home_url()
                )
            ));

            if (is_wp_error( $response ) || $response['response']['code'] !== 200)
            {

                $msg = '';

                if( is_array( $response) && isset( $response['body'] ) ) {
                    $obj = json_decode( $response['body'] );
                    $msg = $obj->error->message;
                }

                if( isset( $response->errors) && isset( $response->errors['http_request_failed'] ) ) {
                    $msg = $response->errors['http_request_failed'][0];
                }

                return array( 'items' => array(), 'error' => true, 'msg' => $msg);
            }

            return json_decode( $response['body'] );
        }  
Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter infinustechnology

    (@koppit)

    https://infinus.ca/yotuwp-easy-youtube-embed.zip

    I removed all of the pro features, the tracking, updated the links to point to a video that works to get info about getting an API key, and added the referer portion.

    @koppit I hope you intend to at least somewhat support your “fork” of this plugin, including it getting auto-updates (even if from your own Github, which is possible) the way the original did? In particular, an issue that I’ve noticed this plugin has (and is among a number of plugins I use that have this) is that the text domain is loaded too early. This is evident by multiple entries such as the following in my site’s debug.log:

    [21-May-2025 18:10:59 UTC] PHP Notice:  Function _load_textdomain_just_in_time was called <strong>incorrectly</strong>. Translation loading for the <code>yotuwp-easy-youtube-embed</code> domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the <code>init</code> action or later. Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 6.7.0.) in /sites/example.com/wp-includes/functions.php on line 6121

    This is an annoyance when I need to comb through the debug.log myself to trace problems on my site. Can you fix this in your fork that is what I now have installed?

    Thread Starter infinustechnology

    (@koppit)

    Hmm I did not see this – but I can definitely take a look. I did notice the load_plugin_textdomain function is called inside of a separate function, instead of inside of init. I’ll switch that around, and upload the changes to github.

    Auto updates would require submission to the WordPress, and I’d have to explore the changes I made and making sure it doesn’t have the same slug, etc. It would be a bit more work that I don’t have the time for just this second, but I will certainly update the plugin on our page with this fix asap.

    Thread Starter infinustechnology

    (@koppit)

    Updated – https://github.com/brendancarr/YouTube-Gallery
    I have also updated the plugin page with the new version, please let me know if it solves the error you were seeing.
    https://infinus.ca/plugins/youtube-gallery/

    The issue still seems present. But I also found a global workaround with the doing_it_wrong_trigger_error filter for my sites, so this is even less important to me than it was earlier today. But, it is still worth fixing, since the whole point of the logged message is to inform plugin devs that they need to fix their plugins to work as intended in WP 6.7 and up.

    This fix isn’t working on our site, still getting the error. I removed the old plugin and used the one from the link you provided and 1.3.16 is activated.

    Also, even if we used to have the pro version, that’s gone now?

    Looking forward to a fix!

    Thread Starter infinustechnology

    (@koppit)

    I tried to track down the load_textdomain error, but interestingly enough, even after disabling that part of the code completely I still got the same error. So I left it as-is for now, if you found a workaround to hide the error messages for now then I’ll leave it at that.

    OK, so the pro features…. I don’t have access to those so I actually don’t have an answer for you on that. Since the plugin is abandoned and the original site is down, it means nobody new would have access to it moving forward, period. I’m assuming it’s an additional plugin that extends features, which I can roll into the main version, if I had access to it.

    What is the error you’re getting? I will update the code to keep a copy of any google-related errors and display them on the screen, it will go a long way to help troubleshoot this.

    Can you email me the details?

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Fix for API key invalid referer!’ is closed to new replies.