Shortcode for ACF Options Page Fields
Adds an [acf_option name="phone"] shortcode that prints a value from an ACF options page anywhere, such as a phone number or address in widgets and footers.
Opens WPIDE on
Code
add_shortcode( 'acf_option', function ( $atts ) {
$atts = shortcode_atts( array( 'name' => '', 'default' => '' ), $atts, 'acf_option' );
$name = sanitize_key( $atts['name'] );
if ( '' === $name || ! function_exists( 'get_field' ) ) {
return '';
}
$value = get_field( $name, 'option' );
if ( is_array( $value ) ) {
$value = isset( $value['label'] ) ? $value['label'] : implode( ', ', array_filter( $value, 'is_scalar' ) );
}
if ( is_bool( $value ) ) {
$value = $value ? 'Yes' : 'No';
}
return esc_html( '' === (string) $value ? $atts['default'] : (string) $value );
} );
Notes
Text, number, email, URL, select and true/false fields work. Use default="..." for a fallback.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.