Last active
February 8, 2022 03:11
-
-
Save DaveyJake/97227f45d4049ee628e49fdb60960797 to your computer and use it in GitHub Desktop.
Retrieve ACF data if plugin is ever deactivated or uninstalled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Should ACF ever become disabled or deactivated for any reason | |
* these functions will ensure that nothing dies in the process. | |
* | |
* These functions were inspired by {@author Steve Grunwell} and | |
* {@author Bill Erickson}. Their work can be viewed, respectively, | |
* at {@link https://stevegrunwell.com} and | |
* {@link https://www.billerickson.net}. | |
* | |
* @author Davey Jacobson <daveyjake21 [at] geemail [dot] com> | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) exit; // exit if directly accessed | |
if ( ! function_exists( 'get_acf_field' ) ) { | |
/** | |
* Get a custom field stored in the ACF Plugin. | |
* | |
* By running this function, we ensure that we don't die if ACF is | |
* uninstalled/disabled (and thus the function is undefined). | |
* | |
* @author Steve Grunwell | |
* @link https://stevegrunwell.com/blog/using-advanced-custom-fields-for-wordpress | |
* | |
* @global WP_Post|object $post The current post object. | |
* | |
* @see get_field() | |
* | |
* @param string $key The key to look for (e.g. field name) | |
* @param int|string $id The post ID (int|str, defaults to $post->ID) | |
* @param mixed $default What to return if there's nothing | |
* | |
* @return mixed | |
*/ | |
function get_acf_field( $key, $id = false, $default = '' ) { | |
global $post; | |
$key = trim( filter_var( $key, FILTER_SANITIZE_STRING ) ); | |
$result = ''; | |
if ( function_exists( 'get_field' ) ) | |
{ | |
$result = ( isset( $post->ID ) && !$id ? get_field( $key ) : get_field( $key, $id ) ); | |
if ( '' === $result ) { | |
$result = $default; | |
} | |
} | |
else // get_field() is undefined, most likely due to the plugin being inactive | |
{ | |
$current_field_value = ( isset( $post->ID ) && !$id ? get_post_meta( $post->ID, $key, true ) : get_post_meta( $id, $key, true ) ); | |
if ( is_int( $current_field_value ) && 'attachment' === get_post_type( $current_field_value ) ) | |
{ | |
$type = get_post_mime_type( $current_field_value ); | |
if ( '' === $default ) | |
{ | |
if ( strpos( $type, 'image' ) ) | |
{ | |
$default = wp_get_attachment_image_src( $current_field_value, 'large' ); | |
} | |
else | |
{ | |
$default = wp_get_attachment_url( $current_field_value ); | |
} | |
} | |
} | |
else | |
{ | |
if ( '' === $default ) | |
{ | |
$default = $current_field_value; | |
} | |
} | |
$result = $default; | |
} | |
if ( '' === $result ) | |
{ | |
if ( is_user_logged_in() ) | |
{ | |
return "This field contains nothing or doesn't exist (anymore)."; | |
} | |
else | |
{ | |
$result = 'Content coming soon.'; | |
} | |
} | |
return $result; | |
} | |
} | |
if ( ! function_exists( 'get_acf_repeater' ) ) { | |
/** | |
* Get specified `$fields` from the repeater with slug `$key` | |
* | |
* @author Steve Grunwell | |
* @link https://stevegrunwell.com/blog/using-advanced-custom-fields-for-wordpress | |
* | |
* @global WP_Post|object $post The current post instance. | |
* | |
* @see get_custom_field() | |
* @see has_sub_field() | |
* @see get_sub_field() | |
* | |
* @param string $key The custom field slug of the repeater. | |
* @param array $fields The sub-fields to retrieve. | |
* @param integer $id The post ID (will use global `$post` if not specified). | |
* | |
* @return array | |
*/ | |
function get_acf_repeater( $key, $fields = array(), $id = null ) { | |
global $post; | |
if ( ! $id ) $id = $post->ID; | |
$values = array(); | |
if ( ! is_admin() ) { // If ACF is ever uninstalled and/or we're on the frontend. | |
/** | |
* Retrieve `repeater field` content without ACF | |
* | |
* @author Bill Erickson | |
* @link https://www.billerickson.net/advanced-custom-fields-frontend-dependency/#gist14349968 | |
*/ | |
$rows = get_post_meta( $id, $key, true ); // <= The number of total `$rows`. | |
if ( $rows ) | |
{ | |
$value = array(); | |
for ( $i = 0; $i < $rows; $i++ ) | |
{ | |
foreach ( $fields as $field ) | |
{ | |
$meta_key = "{$key}_{$i}_{$field}"; | |
$meta_value = get_post_meta( $id, $meta_key, true ); | |
$meta_value = do_shortcode( $meta_value ); | |
$value[ $field ] = $meta_value; | |
if ( ! empty( $value ) ) | |
{ | |
$values[ $i ] = $value; | |
} | |
} | |
} | |
} | |
} | |
elseif ( get_acf_custom_field( $key, $id, false ) && | |
function_exists( 'has_sub_field' ) && | |
function_exists( 'get_sub_field' ) ) { | |
while ( has_sub_field( $key, $id ) ) { | |
$value = array(); | |
foreach ( $fields as $field ) { | |
$value[ $field ] = get_sub_field( $field ); | |
} | |
if ( ! empty( $value ) ) { | |
$values[] = $value; | |
} | |
} | |
} | |
return $values; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment