Frage

I'm using ACF (Advance Custom Fields) plugin in my theme. This plugin provides a simpler methods get_field() which we can use in place of WordPress native get_post_meta() method. Now, the issue is the get_field() method works fine if ACF plugin is active, but when the plugin is not active the theme front-end throughs the following error:

Fatal error: Call to undefined function get_field()

Now, I tried to provide a fallback function, which should be used when the plugin is not active, my fallback function is below:

if ( !function_exists('get_field') ) {

    function get_field($key) {
        return get_post_meta(get_the_ID(), $key, true);
    }
}

add_action( 'after_setup_theme', 'get_field' );

Now, this function do work when the ACF plugin is not active but as soon as I try to activate the ACF plugin the backend throughs an error that:

Fatal error: Cannot redeclare get_field()

Now, I want to use my fallback function ONLY when plugin is not active and it shoud not stop activation of the ACF plugin afterwards.

What could be the solution?

War es hilfreich?

Lösung 2

The issue has been solved. It seems ACF plugin defined get_field() function is not able to override the theme defined get_field() function on ACF plugin activation. Therefore, it throws Fatal error: Cannot redeclare get_field() error if we use the following code:

if ( !function_exists('get_field') ) {

    function get_field($key) {
        return get_post_meta(get_the_ID(), $key, true);
    }
}

Now since, before activating ACF plugin, we only need this custom wrapper function in front-end of our theme we can limit it's registration only to the front-end like this:

if ( !is_admin() && !function_exists('get_field') ) {

    function get_field($key) {
        return get_post_meta(get_the_ID(), $key, true);
    }

}

Now, our backend is not aware of our custom defined get_field() function so it won't cause any issue while activating the plugin and after activating the plugin the ACF plugin's get_field() function will take over as expected.

Andere Tipps

The after_setup_theme hook is actually an action hook not a filter hook. Nonetheless that should not be needed.

Give this a try:

if( ! function_exists( 'get_field' ) ) {
    function get_field( $key, $post_id = false, $format_value = true ) {
        if( false === $post_id ) {
            global $post;
            $post_id = $post->ID;
        }

        return get_post_meta( $post_id, $key, true );
    }
}

this answers are not good. as get_field does a lot more than getting post_meta it also gets options. best bet is for you to shim the get_field function from core into the theme using the if ( !is_admin() && !function_exists('get_field') ) {} you may have to modify certain things the function is doing such as calling other acf hooks and functions. My preferred options is to not use get_field and use the wp core functions to get the post, comment,user meta and the get_option function. it will decouple your front end from the plugin as well as speed it up.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top