Skip to content

Instantly share code, notes, and snippets.

@hereswhatidid
Last active December 19, 2015 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hereswhatidid/6023589 to your computer and use it in GitHub Desktop.
Save hereswhatidid/6023589 to your computer and use it in GitHub Desktop.
Extends the default Advanced Custom Fields get_field function to allow options for a default value and fallback to global options value.
<?php
if ( ! function_exists( 'get_field_ext' ) && ( function_exists( 'get_field' ) ) ) {
/**
* Extends the default ACF get_field function to allow options for a default value and fallback to global option
*
* @author Gabe Shackle <gabe@hereswhatidid.com>
* @param string $name Field name
* @param mixed $default Default value if none found
* @param integer $id Post ID to check the field value of
* @return mixed Value after running checks
*/
function get_field_ext( $name = '', $default = null, $id = null ) {
if ( ! empty( $id ) ) {
if ( $result = get_field( $name, $id ) ) {
return $result;
}
} else {
if ( ( $result = get_field( $name ) ) || ( $result = get_field( $name, 'options' ) ) ) {
return $result;
}
}
return $default;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment