ACF link field values are lost after database migration

ACF link field values lost after database migration

Migrating a WordPress database from production to local, or between servers, is routine. But ACF Link fields store absolute URLs inside serialized arrays, which means a plain search-and-replace in phpMyAdmin will corrupt the data. Here’s how to handle it properly.

Problem: When using ACF, some field values, such as Link fields, are lost after importing a database with a changed domain.

Why the values disappear

A Link field is saved as a serialized PHP array, for example a:3:{s:5:"title";s:4:"Home";s:3:"url";s:19:"https://example.com";…}. Every string is stored together with its length in bytes. A plain SQL REPLACE() changes the URL but not the stored length, so PHP can no longer unserialize the value and ACF shows an empty field.

Interestingly, if you use relative links, everything keeps working, but that’s rarely practical.

Replace the domain with a serialization-aware tool

A much better approach is to use the Search & Replace for WordPress Databases utility. After downloading it, place the folder in the root of your site and rename it to something like replace for convenience. Then open http://yourdomain/replace in a browser, fill in both site URLs, and click “Live Run”. Delete the folder as soon as you’re done: anyone who finds it can rewrite your database.

If you have WP-CLI, the same can be done in one command, and it also handles serialized data:

wp search-replace 'https://old-domain.com' 'https://new-domain.com' --dry-run

Remove --dry-run when the report looks right.

Output the Link field safely

For cleaner output, you can also use this helper function:

function the_custom_acf_link( $acf_link_field, $class = '' ) {
    if ( empty( $acf_link_field['url'] ) ) {
        return;
    }

    $target = ! empty( $acf_link_field['target'] ) ? $acf_link_field['target'] : '_self';
    $rel    = '_blank' === $target ? ' rel="noopener"' : '';
    $class  = $class ? ' class="' . esc_attr( $class ) . '"' : '';

    printf(
        '<a href="%s"%s target="%s"%s>%s</a>',
        esc_url( $acf_link_field['url'] ),
        $class,
        esc_attr( $target ),
        $rel,
        esc_html( $acf_link_field['title'] )
    );
}

Alternative: separate fields for URL, title and target

Sometimes it’s preferable to use two separate ACF text fields for the URL and the title, plus a true/false field for the target (e.g. “Open in new tab”). You can then write a helper function to output them. It has a different name, so both helpers can live in the same theme:

function the_custom_acf_link_fields( $title, $url, $new_tab, $class = '' ) {
    if ( ! $url ) {
        return;
    }

    $target = $new_tab ? ' target="_blank" rel="noopener"' : '';
    $class  = $class ? ' class="' . esc_attr( $class ) . '"' : '';

    printf( '<a href="%s"%s%s>%s</a>', esc_url( $url ), $class, $target, esc_html( $title ) );
}

NOTE: Always run the search-and-replace against a database backup first and verify the results before touching production.

Sources:

  1. https://www.advancedcustomfields.com/resources/link/
  2. https://support.advancedcustomfields.com/forums/topic/link-field-values-are-lost-after-database-migration/
  3. https://interconnectit.com/search-and-replace-for-wordpress-databases/