Support » Plugin: Picturefill.WP » Not Working with ACF

  • I’m having trouble understanding where to put the code and what parts to customize in order to get this to work with ACF. I have an acf image field named ‘banner’. Here is my code:

    Theme File:

    <?php
    $image_object = get_field('banner');
    $image_output = '<img src="' . $image_object['sizes']['medium'] . '" title="' . $image_object['title'] . '" alt="' . $image_object['alt'] . '" />';
    echo apply_filters('theme_acf_image', $image_output, 'name_of_the_image_field');
    ?>

    functions.php

    function theme_function_for_acf_image($content, $name_of_the_image_field){
      return Picturefill_WP::get_instance()->cache_picturefill_output($content, $name_of_the_image_field);
    }

    All that outputs is the letter h. If I echo $image_object it outputs the proper url of the uploaded image. Any input is much appreciated.

    https://wordpress.org/plugins/picturefillwp/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author kylereicks

    (@kylereicks)

    “h”? That’s a new one. The first thing to check might be the “Return Value” under the field settings in ACF. If echoing $image_object outputs an image url, then the return value is probably set to “URL”. The code you provided expects $image_object to be set to “Image Object” an array of data about the image (sizes, attributes, etc.).

    Next we should replace “name_of_the_image_field” with the name of the image field, in the theme file. In this case “banner”. We can leave $name_of_the_image_field alone in the functions.php file since it’s value is being passed to it.

    Finally, we will want to make sure that the filter is also being added in the functions.php file.

    add_filter('theme_acf_image', 'theme_function_for_acf_image', 10, 2);

    Please let me know if that gets us any closer to where we want to be.

    Thanks very much,

    Is there a solution for this problem? Code examples?

    I’ve finally managed to crack this (I think…) by following a few of kylereicks posts.

    Step 1:
    I’ve downloaded picturefill2.js from github to install the plugin “Picturefill.WP 2” into WordPress. I then specified the image sizes using my theme css breakpoints in the WordPress admin settings > media. To resize any existing images in the media library to match the new image size settings I used the “Regenerate Thumbnails” plugin.

    Step 2:
    Was to create a custom image field in ACF, which for this example I’ve called “your_custom_image_fieldname”. The Field Return Value I have set to “image URL”.

    Step 3:
    In the functions.php I have added:

    add_filter('theme_acf_image', 'theme_function_for_acf_image', 10, 2);
    
    function theme_function_for_acf_image($content, $name_of_the_image_field){
      return Picturefill_WP::get_instance()->cache_picturefill_output($content, $name_of_the_image_field);
    }

    I didn’t need to make any changes to this and it was a straight copy and paste.

    Step 4:
    In the theme’s template file I added:

    <?php
    $image_url = get_field('your_custom_image_fieldname');
    $image_output = '<img src="' . $image_url . '" />';
    echo apply_filters('theme_acf_image', $image_output, 'your_custom_image_fieldname');
    ?>

    So as you can see, you need to replace “your_custom_image_fieldname” in two places with the name of your particular ACF image field’s name. You can also specify any id or class within the img tag if you want.

    This seems to be very simple compared to some other solutions I found, but this was the only way I could get it to work.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Not Working with ACF’ is closed to new replies.