2

If I try to use ACF inside my custom functions plugin I get this error:

Uncaught Error: Call to undefined function get_field() in

I created my custom function plugin to avoid using the default functions.php that comes with the theme, because it overrides on every update.

My code:

<?php

$author_id = get_the_author_meta('ID');
$author_badge = get_field('post_autor', 'user_'. $author_id );

?>
<img src="<?php echo $author_badge['url']; ?>" alt="<?php echo $author_badge['alt']; ?>" />

I'm using a Gantry5 theme.

I already followed the documentation and read some posts with examples, but even so I’m not being able to get it working. The field is set as User Object.

UPDATE:

I found this code in the developer's website, I'm going to be giving it a try. This code supposed to allow ACF (free version) to be loaded inside a plugin.

// Define path and URL to the ACF plugin.
define( 'MY_ACF_PATH', get_stylesheet_directory() . '/includes/acf/' );
define( 'MY_ACF_URL', get_stylesheet_directory_uri() . '/includes/acf/' );

// Include the ACF plugin.
include_once( MY_ACF_PATH . 'acf.php' );

// Customize the url setting to fix incorrect asset URLs.
add_filter('acf/settings/url', 'my_acf_settings_url');
function my_acf_settings_url( $url ) {
    return MY_ACF_URL;
}

// (Optional) Hide the ACF admin menu item.
add_filter('acf/settings/show_admin', 'my_acf_settings_show_admin');
function my_acf_settings_show_admin( $show_admin ) {
    return false;
}

https://www.advancedcustomfields.com/resources/including-acf-within-a-plugin-or-theme/

Second Update: Still having the same problem.

PickleRiiiick
  • 31
  • 1
  • 4
  • I installed via wp-admin, normal procedure, if that's the problem. – PickleRiiiick Apr 17 '21 at 08:44
  • I have just uninstalled and reinstalled the plugin, but this time I manually uploaded it from the file that I downloaded from wordpress plugins directory. Still the very same problem. Don't you have any idea of what it might be? – PickleRiiiick Apr 17 '21 at 09:07
  • What a coincidence, this is really a small world. I also know how to use google, but unfortunately I couldn't solve my problem this way. :( – PickleRiiiick Apr 17 '21 at 09:13
  • I already reported it, it's been 2 days and I didn't get a reply. – PickleRiiiick Apr 17 '21 at 09:17
  • `Call to undefined function get_field()` means that it doesn't recognize the function which leads in the direction of misconfigured/disabled plugin. – Ivan86 Apr 17 '21 at 09:17
  • I've used that plugin just fine until the day I needed to display a field inside the theme. – PickleRiiiick Apr 17 '21 at 10:08
  • Did you add ACF to your custom functions plugin to make sure that it's loaded by the time you call the get_field function? You can also probably do some kind of workaround where you get the field from within your theme and then pass it in to the custom plugin? – Ivan86 Apr 17 '21 at 18:34
  • I tried using `plugins_loaded`, but then I received another error. – PickleRiiiick Apr 18 '21 at 10:49
  • I'm updating the question now. – PickleRiiiick Apr 18 '21 at 11:11
  • Nothing wrong to create a plugin for extra functionality, but if this is just extending the functions.php of a theme, you might want to consider creating a child theme instead. https://developer.wordpress.org/themes/advanced-topics/child-themes/#using-functions-php – Zoli Szabó Apr 18 '21 at 11:18
  • 2
    In any case, you need to make sure the ACF plugin is loaded before you can call functions exposed by it. THe `plugins_loaded` or `init` are generally the hooks you want to use. – Zoli Szabó Apr 18 '21 at 11:20
  • @ZoliSzabó I'm trying to display this in a child theme, but the php part I need it to be inside the custom functions plugin, because my theme uses twig. :( – PickleRiiiick Apr 18 '21 at 13:30
  • @ZoliSzabó Using Gantry5's Helium theme. – PickleRiiiick Apr 18 '21 at 13:31

0 Answers0