WordPress.org

Ready to get started?Download WordPress

Forums

Advanced Custom Fields
[resolved] get_field() returning 'false' in functions.php (5 posts)

  1. daphatmac
    Member
    Posted 1 year ago #

    Hey mate - I've been having weird issues with the get_field() function since 3.4.1 (and 3.4.2) - I was hoping that you could spare 5 minutes to clarify if this is me, or an issue.

    Basically, I have some simple housekeeping functions that I run in my theme's functions.php file and up to 3.4.1, they worked fine, but since the latest updates, instead of actual data, I'm getting 'false' returned for get_field() calls, but ONLY for those that I make in the functions.php file. Any calls to get_field() in the rest of the site work fine.

    I've done some quick digging and I've noticed that if I do a var_dump($this->fields); in the get_value_for_api() function, I get 'null' instead of the object containing the registered field types. If I do a var_dump at the init of the plugin, the $acf->fields object seems to be set OK, but by the time it gets to the get_value_for_api() call it seems to be set to 'null' - obviously that then returns 'false' for the whole get_field() call.

    I'd really appreciate any ideas you might have on this. I thought maybe that a custom field type that I use might have been incompatible with the updates, and disabled it, but there was no difference.

    Thanks in advance!

    Regards,

    George

    http://wordpress.org/extend/plugins/advanced-custom-fields/

  2. daphatmac
    Member
    Posted 1 year ago #

    Hmmm. I've rolled back to 3.2.0 and a previous version of my DB and I'm still having similar issues. Let me check it out some more and I'll get back to you.

    Thanks & great plugin BTW!

  3. daphatmac
    Member
    Posted 1 year ago #

    Yeah, it seems that $acf is only partly instantiated by the time I need it in functions.php. If I global it, I can get to it, but it's clearly not initialized in full yet. I hadn't noticed it before because I had some fallback code in case of an empty return from the get_field(), which was covering for it.

    I've done some searching and apparently plugins are loaded and init'ed before the functions.php file, so I should have access to the ACF methods in full. Is there any way to make sure the fields are registered and the plugin is fully available, or is this just a limitation of WP?

    Thanks again!

  4. ococa252
    Member
    Posted 1 year ago #

    Hi there, i'm also interesting this..

    I'm using wp_localize_script to pass variables into my main.js file.
    when i console.log() the variable nothing logs.

    cheers.

    sample code;
    PHP inside function.php

    $vimeo_embed = get_field('vimeo_embed');
    wp_localize_script( 'main', 'wp', array( 'HOME_VIMEO_EMBED_SCRIPT' => $vimeo_embed));

    JS
    console.log(String(wp.HOME_VIMEO_EMBED_SCRIPT));

  5. elliotcondon
    Member
    Plugin Author

    Posted 1 year ago #

    Hi Guys,

    All ACF API calls need to happen in an action after init.

    Use wp_head or admin_head or any other actions here:
    http://codex.wordpress.org/Plugin_API/Action_Reference

    Good luck

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic