Skip to content

Instantly share code, notes, and snippets.

@coreyworrell
Last active July 28, 2017 20:18
Show Gist options
  • Save coreyworrell/b3c04e56d665bc40fcf60f2d8e7eef4c to your computer and use it in GitHub Desktop.
Save coreyworrell/b3c04e56d665bc40fcf60f2d8e7eef4c to your computer and use it in GitHub Desktop.
Migrate ACF Link plugin data to ACF 5.6.0

Migrate ACF Link plugin data to ACF Pro 5.6.0

The new version of ACF Pro (5.6.0) adds a built-in link selector field. This makes the ACF Link plugin obsolete.

One problem though is that the ACF Link plugin saved the link's target value as a boolean (0 or 1), and ACF now saves it as an empty string ("") or as _blank.

Disclaimer

Please please please, backup your database before running this script. I have tested many times, but different WordPress installations and platforms could cause different behavior. Run at your own risk, as we cannot be responsible if the script does not work correctly in all situations.

How to use

Paste code

The following code can be placed in your theme's functions.php to run a quick migration to preserve the target attributes across all your fields. It will work for Comments, all Posts/Post Types, Terms, Users, and Options (all other location rules for ACF are stored as Options).

Load page, run migration

After pasting code in your theme, load any page of your site with ?acf_link_migration appended to the URL. This will do a dry-run of the migration, showing what will be done. If all looks good you can move to next step.

Now append another URL parameter to actually run the migration: &run_migration

So you should have something like: http://mysite.com/?acf_link_migration&run_migration

Clean up

After that runs once, your database will be updated and you can now remove the code from your functions.php file. All fields will now be 100% working with ACF Pro's new Link field.

If you have any problems or questions, please open a ticket.

<?php
add_action('init', function () {
global $wpdb;
if ( ! isset($_GET['acf_link_migration'])) {
return;
}
$run = isset($_GET['run_migration']);
$formatTarget = function ($target) {
if ($target === '1') {
$target = '_blank';
} elseif ($target === '0') {
$target = '';
}
return $target;
};
$replaceCommentMeta = function ($name) use ($wpdb, $run, $formatTarget) {
echo "\nMigrating comments\n";
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->commentmeta WHERE meta_key = %s", $name), ARRAY_A);
foreach ($rows as $row) {
$value = maybe_unserialize($row['meta_value']);
if (is_array($value) && isset($value['target'])) {
$orig = $value['target'];
$value['target'] = $formatTarget($value['target']);
if ($orig !== $value['target']) {
echo " Updating comment #{$row['comment_id']} link target from '{$orig}' to '{$value['target']}'\n";
if ($run) {
update_field($name, $value, 'comment_'.$row['comment_id']);
}
}
}
}
};
$replacePostmeta = function ($name) use ($wpdb, $run, $formatTarget) {
echo "\nMigrating posts\n";
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_key = %s", $name), ARRAY_A);
foreach ($rows as $row) {
$value = maybe_unserialize($row['meta_value']);
if (is_array($value) && isset($value['target'])) {
$orig = $value['target'];
$value['target'] = $formatTarget($value['target']);
if ($orig !== $value['target']) {
echo " Updating post #{$row['post_id']} link target from '{$orig}' to '{$value['target']}'\n";
if ($run) {
update_field($name, $value, $row['post_id']);
}
}
}
}
};
$replaceTermmeta = function ($name) use ($wpdb, $run, $formatTarget) {
echo "\nMigrating terms\n";
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->termmeta WHERE meta_key = %s", $name), ARRAY_A);
foreach ($rows as $row) {
$value = maybe_unserialize($row['meta_value']);
if (is_array($value) && isset($value['target'])) {
$orig = $value['target'];
$value['target'] = $formatTarget($value['target']);
if ($orig !== $value['target']) {
echo " Updating term #{$row['post_id']} link target from '{$orig}' to '{$value['target']}'\n";
if ($run) {
update_field($name, $value, 'term_'.$row['term_id']);
}
}
}
}
};
$replaceUsermeta = function ($name) use ($wpdb, $run, $formatTarget) {
echo "\nMigrating users\n";
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE meta_key = %s", $name), ARRAY_A);
foreach ($rows as $row) {
$value = maybe_unserialize($row['meta_value']);
if (is_array($value) && isset($value['target'])) {
$orig = $value['target'];
$value['target'] = $formatTarget($value['target']);
if ($orig !== $value['target']) {
echo " Updating user #{$row['user_id']} link target from '{$orig}' to '{$value['target']}'\n";
if ($run) {
update_field($name, $value, 'user_'.$row['user_id']);
}
}
}
}
};
$replaceOptions = function ($name) use ($wpdb, $run, $formatTarget) {
echo "\nMigrating options\n";
$rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->options WHERE option_name = %s", $name), ARRAY_A);
foreach ($rows as $row) {
$value = maybe_unserialize($row['meta_value']);
if (is_array($value) && isset($value['target'])) {
$orig = $value['target'];
$value['target'] = $formatTarget($value['target']);
if ($orig !== $value['target']) {
echo " Updating option #{$row['option_name']} link target from '{$orig}' to '{$value['target']}'\n";
if ($run) {
update_field($name, $value, 'option_'.$row['option_name']);
}
}
}
}
};
header('Content-Type: text/plain');
if ( ! $run) {
echo "THIS IS A DRY-RUN/SIMULATION\n";
echo "Append `&run_migration` to the URL to actually run the migration\n";
echo "after you have verified what will happen is correct\n";
}
echo "\nMigration begin\n";
foreach (acf_get_field_groups() as $group) {
foreach (acf_get_fields($group) as $field) {
if ($field['type'] !== 'link') {
continue;
}
echo "\n----------\n";
echo "\nFound field: {$field['label']} [{$field['name']}:{$field['key']}]\n";
$name = $field['name'];
$replaceCommentMeta($name);
$replacePostmeta($name);
$replaceTermmeta($name);
$replaceUsermeta($name);
$replaceOptions($name);
}
}
echo "\nMIGRATION COMPLETE!\n";
die;
});
@JacobDB
Copy link

JacobDB commented Jul 28, 2017

Great work, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment