Skip to content

Instantly share code, notes, and snippets.

@mosne
Last active August 17, 2018 18:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosne/75b3ce1f0098663f2b1a to your computer and use it in GitHub Desktop.
Save mosne/75b3ce1f0098663f2b1a to your computer and use it in GitHub Desktop.
Migrate images form a Magic Fields to ACF (inside a wordpress loop)
<?php
$photo_name = get_field("cover");
if ($photo_name != "") {
$ff = '/Users/mosne/Sites/Dropbox/mosne.local/wp-content/files_mf/' . $photo_name;
$handle = fopen($ff, "r");
$ffcontents = fread($handle, filesize($ff));
$attachment = wp_upload_bits($photo_name, null, $ffcontents);
$filetype = wp_check_filetype(basename($attachment['file']), null);
$postinfo = array(
'post_mime_type' => $filetype['type'],
'post_title' => $post->post_title . ' ' . "cover",
'post_content' => '',
'post_status' => 'inherit'
);
echo $filename = $attachment['file'];
echo $attach_id = wp_insert_attachment($postinfo, $filename, $post->ID);
if (!function_exists('wp_generate_attachment_data'))
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
update_post_meta($post->ID, "_cover_new", "field_53aed7e7a7ff5");
update_post_meta($post->ID, "cover_new", $attach_id);
update_field("field_53aed7e7a7ff5", $attach_id, $post->ID);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment