YouTube Videos Without Cookies on WP Sites Now

Cookies! A bit of a headache in the world of web development since the GDPR was implemented in May 2018.

Let me show you how to easily remove all YouTube cookies from your site right away, even if it has thousands of videos embedded already — in which case of course you don’t want to remove them manually.

Please sit down, make yourself comfortable and visit a video of your choice; here are the steps to embed a video with no cookies just in case you didn’t know.

Figure 1. Click on the Share button below the video

Figure 2. Select the Embed option

Figure 3. Check the box that says Enable privacy-enhanced mode

Figure 4. Copy the iframe to your clipboard

You see, the resulting iframe is the same as the one with cookies except it uses the domain name www.youtube-nocookie.com rather than www.youtube.com.

www.youtube-nocookie.com is the no cookie version of www.youtube.com.

Millions of Cookies

Here is a hack to implement the four-step process above on a thousand pages website created with WordPress. The idea is very similar for any other CMS like Drupal, Joomla, Magento, etc.

First things first we need the IDs of the published posts only:

select ID from wp_posts where post_status = 'publish' order by post_modified desc;

So at this point we’re in a position to address the question:

How many posts are currently embedding a YouTube video with cookies?

SELECT COUNT(ID) FROM wp_posts
WHERE post_content LIKE '%https://www.youtube.com/embed/%'
AND ID IN (select ID from wp_posts where post_status = 'publish' order by post_modified desc);

A bit more specifically:

mysql> SELECT COUNT(ID) FROM wp_posts
-> WHERE post_content LIKE '%https://www.youtube.com/embed/%'
-> AND ID IN (select ID from wp_posts where post_status = 'publish' order by post_modified desc);
+-----------+
| COUNT(ID) |
+-----------+
| 2880 |
+-----------+
1 row in set (0.16 sec)

Mmm. 2880 posts with YouTube videos, not too bad.

Let’s say they are to be updated manually by a human being like you and me, one minute to change each of them. Then it’d take 2,880 minutes = 48 hours. 5 to 9 from Monday to Friday is 48 / 8 = 6 working days to complete the task.

Well isn’t that fascinating?

At this point the vast majority of web developers (me included) will try to just transform all https://www.youtube.com/embed/ into https://www.youtube-nocookie.com/embed/ at once as shown below.

UPDATE wp_posts
SET post_content = REPLACE (post_content, 'https://www.youtube.com/embed/', 'https://www.youtube-nocookie.com/embed/')
WHERE post_content LIKE '%https://www.youtube.com/embed/%'
AND ID IN (select ID from wp_posts where post_status = 'publish' order by post_modified desc);

But whoops! This error is thrown:

ERROR 1093 (HY000): You can't specify target table 'wp_posts' for update in FROM clause

Remember, the wp_posts table cannot be updated because it’s being used in a SELECT statement as it is documented at 13.2.11 UPDATE Syntax:

You cannot update a table and select from the same table in a subquery.

We need to create a temporary table to store the IDs of the published posts:

CREATE TEMPORARY TABLE IF NOT EXISTS temp_wp_posts_ids
SELECT ID from wp_posts where post_status = 'publish' order by post_modified desc;

And finally run:

UPDATE wp_posts
SET post_content = REPLACE (post_content, 'https://www.youtube.com/embed/', 'https://www.youtube-nocookie.com/embed/')
WHERE post_content LIKE '%https://www.youtube.com/embed/%'
AND ID IN (select ID from temp_wp_posts_ids);

The MySQL REPLACE() function is really handy!

Congratulations. We did it in a snap. You’ve learned a hack on how to update your site’s content right now.

You may also be interested in…