Support

Account

Home Forums Backend Issues (wp-admin) Problems including ACF in my theme

Solved

Problems including ACF in my theme

  • I’m trying to include ACF in my theme, following the instructions here: https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/

    Some of it is working, and some of it is not, and I’m mystified as to why.

    I know that I have included the files correctly, and that the filters to customize the path and directory are working. However, I see ACF in the dashboard, which means that the filter to hide ACF is not working. Also, I have registered a bunch of custom fields with PHP, and none of those are showing up.

    Here is my code:

    /**
     * Change the Advanced Custom Fields settings path.
     *
     * @param $dir
     * @return string
     */
    function zephyr_acf_settings_path( $path ) {
        $path = get_template_directory() . '/lib/acf/';
        return $path;
    }
    add_filter( 'acf/settings/path', 'zephyr_acf_settings_path' );
    
    /**
     * Change the Advanced Custom Fields settings directory.
     *
     * @param $dir
     * @return string
     */
    function zephyr_acf_settings_dir( $dir ) {
        $dir = get_template_directory_uri() . '/lib/acf/';
        return $dir;
    }
    add_filter( 'acf/settings/dir', 'zephyr_acf_settings_dir' );
    
    /**
     * Hide the ACF UI.
     */
    add_filter( 'acf/settings/show_admin', '__return_false' );
    
    /**
     * Generate ACF metaboxes.
     */
    function zephyr_acf_add_local_field_groups() {
    
        // PHP code copied from ACF goes here
    
    }
    add_action( 'acf/init', 'zephyr_acf_add_local_field_groups', 1 );

    What’s extra weird is that I know WordPress recognizes my filters. If I do this:

    if( array_key_exists( 'acf/init' , $GLOBALS['wp_filter']) ) {
        wp_die( '<pre>' . print_r( $GLOBALS['wp_filter'], true ) . '</pre>');
    }

    all of the filters show up with the correct functions and priorities, but somehow the filters aren’t actually happening. Has anyone else seen this? Any idea what I am doing wrong?

  • Try increasing the priority for the admin. There could be another filter that overriding yours

    
    add_filter( 'acf/settings/show_admin', '__return_false', 20 );
    

    For your fields, try a different hook

    
    add_action( 'acf/include_fields’', 'zephyr_acf_add_local_field_groups', 1 );
    

    This may not work. This issue is usually caused by some other plugin or some other code that is trying to get a list of field groups or fields before you are trying to include your fields. What happens is this: when they get the fields ACF puts the fields into the WP meta cache. Then you you add your fields the cache is not cleared. The next time the fields are retrieved the cached version used which does not include your fields. The first thing that I’d do is figure out if there is something else that causing this. If all else fails you can attempt to clear the cache before you add your fields, however, this may cause issues with whatever it is that’s causing this issue. This may work, but it’s been a while since I’ve had to use it and I’m not 100% certain that it’s the right cache key wp_cache_delete('get_field_groups', 'acf');

  • Hi, John,

    Thanks for the advice!

    Unfortunately, none of this helps. I have tried using my code on a fresh WordPress site, with no plugins installed, using the TwentyFifteen theme, and I get the same results, so it’s not a plugin conflict or a priority issue (and I have also tried changing the priority). Clearing the cache seemed promising, but didn’t change anything.

    Using define( 'ACF_LITE', true ); to hide the UI works, but doesn’t solve the problem of my fields not being registered.

  • I’m still not sure what was going on, but updating to the latest version of ACF seems to have fixed it.

  • Sorry I did not get back to this, I have been meaning to set up a test to see if I could recreate the problem and figure out what was going on, but in this case setting up the test would be a long process and I did not find the time.

    I’m glad that the update corrected it, whatever it was.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Problems including ACF in my theme’ is closed to new replies.