Skip to content

Instantly share code, notes, and snippets.

@lgladdy
Created November 4, 2014 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgladdy/3d16dcf87c1cec4c4b6d to your computer and use it in GitHub Desktop.
Save lgladdy/3d16dcf87c1cec4c4b6d to your computer and use it in GitHub Desktop.
ACF 5.1.1 array_key_exists() fix
--- a/wp-content/plugins/advanced-custom-fields-pro/api/api-helpers.php
+++ b/wp-content/plugins/advanced-custom-fields-pro/api/api-helpers.php
@@ -841,6 +841,7 @@ function acf_extract_var( &$array, $key ) {
// vars
$r = null;
+ if (!is_array($array)) return false;
// check if exists
if( array_key_exists($key, $array) ) {
@lgladdy
Copy link
Author

lgladdy commented Nov 4, 2014

This code is executed when ACF tries to get a field value. If the field doesn't exist on the post_id, $array will be false when it's passed into acf_extract_var. This causes the array_key_error. There's no need to try and extract a value from false (or anything but an array)

@elliotcondon
Copy link

Thanks @lgladdy

I've taken the logic and added it to the function. Now looks like:

function acf_extract_var( &$array, $key ) {

    // check if exists
    if( is_array($array) && array_key_exists($key, $array) ) {

        // store value
        $v = $array[ $key ];


        // unset
        unset( $array[ $key ] );


        // return
        return $v;

    }


    // return
    return null;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment