Web dev hints, tips and tricks to help you become an expert.

WordPress makes it easy to embed content from third-party sites, such as tweets, videos, images and audio.

Instead of having to upload the content to your website or copy the embed code directly into your post, you can simply ‘link’ to it and WordPress will retrieve it for you using oEmbed.

If you’re using the block editor you can add an embed block to achieve this, with the classic editor simply place the URL on its own line.

How WordPress uses oEmbed

WordPress’s embed functionality scans for and then communicates with any URLs in a post’s content in order to fetch the relevant code.

It then tries to add the embed code and the time it was retrieved to the post’s metadata.

What happens when oEmbed fails

NEW: Use the free oEmbed Monitor plugin for WordPress to find failed.

Sometimes this procedure doesn’t work, particularly when tweets and older versions of WordPress are involved.

The time may still be correctly stored but the corresponding code metadata’s value may be set as {{unknown}}.

Once it’s cached like this, the embed code may never be retrieved correctly and could display as a plain or linked URL within your post.

How to find failed oEmbed caches

It might be obvious to you that such failed instances are already present in your posts, or you can search for them by querying your site’s database directly:

SELECT
	`meta_key`,
	`meta_value`
FROM
	`wp_postmeta`
WHERE
	`meta_key` LIKE "_oembed_%"
	AND `meta_value` = "{{unknown}}"

These caches do not expire and sometimes never get overwritten.

However if the metadata which has an {{unknown}} value and its _oembed_time_ partner are removed, WordPress will try again to retrieve the embed code the next time the post’s content is filtered (e.g. when the post is viewed).

Identify which posts contain failed oEmbeds

Some guides will encourage you to delete all oEmbed metadata for a particular post or even for all posts on your website.

But doing so runs the risk of getting rid of data which you may never be able to retrieve again, e.g. tweets that have been deleted since you saved their content.

So it’s better to take a targeted approach and delete just the pairs related to the embeds you want to regenerate.

Let’s expand the above query so we can get see which post contains each failed oEmbed:

SELECT
	`wp_posts`.`post_title`,
	`wp_postmeta`.`meta_key`,
	`wp_postmeta`.`meta_value`
FROM
	`wp_postmeta`
	LEFT JOIN `wp_posts` ON `wp_postmeta`.`post_id` = `p`.`ID`
WHERE
	`wp_posts`.`post_type` = "post"
	AND `wp_postmeta`.`meta_key` LIKE "_oembed_%"
	AND `wp_postmeta`.`meta_value` = "{{unknown}}"

Delete failed oEmbed metadata

Now you can find the metadata which governs the embeds you want to fix.

The meta_key with a meta_value of {{unknown}} might look something like _oembed_e44f7ac6ea70759563d1b6cc5e696ecc.

And, for each of these, there will be matching _oembed_time_ metadata which contains the same hash in its meta_key, i.e. _oembed_time_e44f7ac6ea70759563d1b6cc5e696ecc.

Check the third-party content still exists, backup your database and then delete the two records at once:

DELETE FROM 
	`wp_postmeta` 
WHERE 
	`meta_key` = "_oembed_e44f7ac6ea70759563d1b6cc5e696ec" 
	OR `meta_key` = "_oembed_time_e44f7ac6ea70759563d1b6cc5e696ec"

If you want to write a plugin to do this for you, it would be more appropriate to use the delete_metadata or delete_post_meta_by_key functions.

Try to generate new embed code

Providing the content still exists and nothing is interfering with the process, new oEmbed metadata to replace what you just deleted should be generated when filters for the post’s content are next executed.

The easiest way to make this happen is to view the post.

Tags

One response to “How to delete failed oEmbed records in your WordPress database”

  1. […] Once identified, you can remove them as detailed in the guide on how to delete failed oEmbed records in your WordPress database. […]

Leave a Reply

Your email address will not be published. Required fields are marked *