Skip to content

Instantly share code, notes, and snippets.

@AntonLitvin
Forked from oion/acf-helper-functions.php
Created January 28, 2022 09:41
Show Gist options
  • Save AntonLitvin/574b1653ce4a8db632bd97fe61561ddb to your computer and use it in GitHub Desktop.
Save AntonLitvin/574b1653ce4a8db632bd97fe61561ddb to your computer and use it in GitHub Desktop.
Advanced Custom Fields Helper Functions
<?php
/**
* Custom functions for the use of the Advanced Custom Fields plugin.
* These are helper functions that you can include in your theme
* which make it easier to your ACF. The purpose is to allow the
* inclusion of ACF fields while also checking to see if ACF is
* installed and activated. With these functions you may be able to
* deactivate ACF for debugging and your theme will not throw
* errors.
*
*
* @author Steven Slack <steven@s2webpress.com>
*
*/
/**
* Standard ACF Field Echo or Return
*
* @param $field_name the advanced custom field name
* @param $before b
* @param boolean $echo echo or return the field
* @return the custom field input
*/
function the_acf_field( $field_name, $before = '', $after = '', $echo = true ) {
if ( ! function_exists( 'get_field' ) )
return;
$field = get_field( $field_name );
if ( empty( $field ) )
return;
$output = $before . $field . $after;
if ( $echo )
echo $output;
else
return $output;
}
/**
* ACF Text checks to see whether ACF is installed
* and activated.
*
* @param string $field_name Required. the advanced custom field name
* @param string $before Optional. Content to prepend to the text.
* @param string $after Optional. Content to append to the text.
* @param string $fallback Optional. Content to echo or return if Advanced Custom Fields is not activated or installed.
* @param bool $echo Optional, default to true.Whether to display or return.
* @return null|string Null on no title. String if $echo parameter is false.
*/
function acf_text( $field_name, $before = '', $after = '', $fallback = '', $echo = true ) {
// returns the fallback text with the translatable string
if ( ! function_exists( 'get_field' ) )
$field = esc_attr__( $fallback, 'phononics' );
else
$field = get_field( $field_name );
if ( strlen( $field ) == 0 )
return;
$text = $before . $field . $after;
if ( $echo )
echo $text;
else
return $text;
}
/**
* ACF Textarea
*
* @param string $field_name Required - the advanced custom field name
* @param string $before Optional. Content to prepend to the textarea.
* @param string $after Optional. Content to append to the textarea.
* @param string $fallback Optional. Content to echo or return if Advanced Custom Fields is not activated or installed.
* @param bool $echo Optional. Default to true. Whether to display or return.
* @return null|string Null on no title. String if $echo parameter is false.
*/
function acf_textarea( $field_name, $before = '', $after = '', $fallback = '', $echo = true ) {
// check to see if ACF is installed and active
// returns the fallback text with the translatable string
if ( ! function_exists( 'get_field' ) )
$field = esc_textarea( $fallback );
else
$field = esc_textarea( get_field( $field_name ) );
if ( strlen( $field ) == 0 )
return;
$textarea = $before . $field . $after;
if ( $echo )
echo $textarea;
else
return $textarea;
}
/**
* Get the images and the links from the ACF fields
*
* @param $image_field Required. field ID the image object
* @param $size Optional. The image size
* @param $class_name Optional. the class name to apply to the image.
* @return echo html img tag
*/
function acf_the_image( $image_field, $size = 'full', $class_name = '' ) {
if ( ! function_exists( 'get_field' ) )
return;
// get the image object
$image = get_field( $image_field );
// set the $class attribute
if ( $class_name !== '' )
$class = 'class="' . $class_name . '"';
else
$class = null;
// ALT text attribute
if ( $image['alt'] !== '' )
$alt = 'alt="' . $image['alt'] . '"';
else
$alt = null;
if ( $image ) :
echo '<img src="' . $image['sizes'][ $size ] . '" ' . $class . $alt . '/>';
endif;
}
/**
* ACF Page Link Anchor Tag
*
* @param string $field_name Required. ID from ACF field
* @param string $class_name Optional. The CSS class name
* @param string $content Optional. The inner content between the open and closing anchor tags
* @return html string anchor tag
*/
function acf_page_link( $field_name, $class_name = '', $content = '' ) {
if ( ! function_exists( 'get_field' ) )
return;
// get the page url
$url = get_field( $field_name );
if ( empty( $url ) )
return;
if ( $class_name !== '' )
$class = 'class="' . $class_name . '"';
else
$class = null;
if ( $url )
echo '<a href="' . esc_url( $url ) . '" ' . $class . '>' . $content . '</a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment