pabamato
1/6/2020 - 9:58 PM

Custom rewrite for ACF get_field / the_field, add support for default value

Custom rewrite for ACF get_field / the_field, add support for default value

<?php
/**
 * Custom rewrite for ACF get_field() with support for returning a default value.
 *
 * @param  string  $field_name   ACF Field name.
 * @param  string  $default      Default value to return if empty setting.
 * @param  mixed   $post_id      The post_id of which the value is saved against.
 * @param  boolean $format_value Whether or not to format the value.
 * @return string                ACF field value or default.
 */
function op_acf_get_field( $field_name, $default = '', $post_id = false, $format_value = true ) {
	if ( ! function_exists( 'get_field' ) ) {
		return $default;
	}
	return get_field( $field_name, $post_id, $format_value ) ?: $default; //phpcs:ignore
}

/**
 * Custom rewrite for ACF the_field() with support for returning a default value.
 *
 * @param  string  $field_name   ACF Field name.
 * @param  string  $default      Default value to return if empty setting.
 * @param  mixed   $post_id      The post_id of which the value is saved against.
 * @param  boolean $format_value Whether or not to format the value.
 * @return void                  Echoes ACF field value or default.
 */
function op_acf_the_field( $field_name, $default = '', $post_id = false, $format_value = true ) {
	echo op_acf_get_field( $field_name, $default, $post_id, $format_value );//phpcs:ignore
}