How right way for import database if site use Advanced Custom Fields?

Problem: If you use ACF Custom Field, after import database some fields are lost e.g. Link field.

ACF link field values are lost after database migration

Solution: Pretty interesting observation, if you use relative links - all ok. But this, not good solution. So much better way use next utility for import database. After download put it to root of the site. For the sake of brevity and convenience we can rename folder to "replace" or something like that. Next, open in browser address: http://domain_name/replace and just filling in addresses sites and click "Live run". For convenience we can use that function:

function the_custom_acf_link( $acf_link_field, $class ) {
	if ( ! $acf_link_field ) {
		return;
	}

    $link_url = esc_url( $acf_link_field['url'] );
    $link_class = ( $class ) ? ' class = "' . sanitize_html_class( $class ) . '" ' : '';		
	$link_target = $acf_link_field['target'] ? $acf_link_field['target'] : '_self';
    $link_title = esc_html( $acf_link_field['title'] );
	$link = '<a href="' . $link_url . '"' . $link_class . 'target="' . esc_attr( $link_target ) . '">' . $link_title . '</a>';
	echo $link;
}

UPD: Sometimes there are cases when better using two ACF text field for link and link title and one ACF true/false field for set target (e.g. "Open in new tab"). Then we can create custom function for output our fields, for example:

function the_custom_acf_link( $title, $url, $target, $class = '' ) {
	if ( ! $url ) {
		return;
	}
		
	$target = ( $target ) ? '_blank' : '_self';
	$class = ( $class ) ? ' class="' . $class . '" ' : ' ';
	$link = '<a href="' . esc_url( $url ) . '"' . $class . 'target="' . $target . '">' . esc_html( $title ) . '</a>';
	echo $link;
}

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/products/search-and-replace-for-wordpress-databases/

Leave Comment

Your email address will not be published. Required fields are marked *