0

What I am trying to accomplish is: when I save a or update a post, I want to access the Untappd API, grab a link to an image, and update an ACF field with that link. I am attempting to use the acf/save_post hook to accomplish this.

Here is what I have so far:

 function brewImageUpdater( $post_id ){
        require_once 'Pintlabs/Service/Untappd.php';
        $config = array(
            'clientId'     => 'XXXXXX',
            'clientSecret' => 'XXXXXX',
            'redirectUri'  => 'stuff.com',
            'accessToken'  => ''
        );

        $untappdd = new Pintlabs_Service_Untappd($config);

        $brid = get_field('untappd_brewery_id', $post_id);
        $brid= int($brid);

        try {
            $feed = $untappd->breweryInfo($brid);
        } 
        catch (Exception $e) {
            echo 'Unable to connect to the Untappd API<br />';
        }
        $bi = $feed->response->brewery->brewery_label;

        $fkey = "field_5609f19dcec5d";
        update_field($fkey, $bi, $post_id);
    }

    add_action( 'acf/save_post', 'brewImageUpdater', 20);

There exists an ACF field called untappd_brewery_id, which allows me to connect to the Untappd's API. I am using Pintlab's Untappd Class to connect to the API. This code breaks when trying to create a new instance of a the Pintlabs_Service_Untappd class. I have attempted to create a function that creates a new instance of the class and invoke it in this function, but that has not worked for me either. Is it possible to connect to an API when using the acf/save_post web hook?

Any help would be greatly appreciated. Thanks!

1 Answer 1

1

It's because your require_once 'Pintlabs/Service/Untappd.php'; is relative to "current" file and should be absolute path. I don't know where are you using this hook but depending on location it should look something like this:

  1. ABSPATH is your wordpress root dir and 'custom-path' should be a path to Pintlabs dir
    require_once ABSPATH.'custom-path/'.'Pintlabs/Service/Untappd.php';

  2. get_template_directory() if you use it inside your theme (functions.php etc.). Notice the additional '/' after get_template_directory()
    require_once get_template_directory().'/custom-path/'.'Pintlabs/Service/Untappd.php';

  3. plugin_dir_path( __FILE__ ) when inside a plugin. You must use this function in your plugins main file like /my-plugin/my-plugin.php or otherwise you get plugins subdirectory. It's best to store it using define() in plugins main file.

    in my-plugin/my-plugin.php
    define("myplugin_dir",plugin_dir_path( __FILE__ ));

    somewhere else
    require_once myplugin_dir.'/Pintlabs/Service/Untappd.php';

1
  • Thanks! Number 2 for the win. This led to a bigger problem, which was then solved by this post
    – kval87
    Nov 17, 2015 at 6:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.