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?