Skip to content

Instantly share code, notes, and snippets.

@mmousawy
Last active May 6, 2020 23:34
Show Gist options
  • Save mmousawy/6ba0f57e63c9021c6fd20c9195741980 to your computer and use it in GitHub Desktop.
Save mmousawy/6ba0f57e63c9021c6fd20c9195741980 to your computer and use it in GitHub Desktop.
Add oEmbed support to ACF WYSIWYG fields (acf_the_content)
<?php
/**
* Add oEmbed support to ACF WYSIWYG fields.
*
* Fixes issue with WP-native oEmbeds not working in ACF WYSIWYG fields because
* these fields are saved in the database without post IDs. WP's oEmbed works out
* of the box only with direct post content.
*/
add_filter('acf_the_content', 'add_oembed');
function add_oembed($content) {
preg_match_all('/^https?:\/\/.[^\s]+$/m', $content, $matches);
if ($matches && $matches[0]) {
$embedCode = '<div class="embed">' . wp_oembed_get($matches[0][0]) . '</div>';
$content = str_replace($matches[0][0], $embedCode, $content);
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment