Skip to content

Instantly share code, notes, and snippets.

@khoipro
Created April 23, 2024 07:52
Show Gist options
  • Save khoipro/d2d205f0ab4796de00e8617148913d3c to your computer and use it in GitHub Desktop.
Save khoipro/d2d205f0ab4796de00e8617148913d3c to your computer and use it in GitHub Desktop.
WP Sample function convert Youtube URL (ACF) to post content (for using existing Embeds feature in WordPress)
<?php
// For example, logged in to your WordPress and hit http://sample.com/?action=update_videos once
add_action('wp', function() {
if ( !is_user_logged_in() ) return;
if ( empty( $_GET['action'] ) ) return;
if ($_GET['action'] !== 'update_videos') return;
$post_args = [
'post_type' => 'video',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'meta_key' => 'youtube_url'
];
$posts = get_posts($post_args);
foreach ($posts as $post_id) {
$youtube_url = get_field('youtube_url', $post_id);
if ( !empty($youtube_url ) ) {
wp_update_post([
'ID' => $post_id,
'post_content' => $youtube_url
]);
delete_field('youtube_url', $post_id);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment