ACF: Adding a Flexible Module Content Preview Image on Hover

July 4, 2019
Wordpress
LinkedInTwitterFacebookEmail

If you’re building custom templates for WordPress for your clients, you most likely have used Advanced Custom Fields. A fantastic little plugin (you’ll need the Pro version for this article), ACF can speed up theme development significantly by allowing you to easily add fields for the client to edit.

The best part of the Pro version of ACF is the Flexible Content Field. Most theme builders come with a “visual builder”. While it allows an amazing amount of freedom in how you lay out each page, it can also provide just enough rope to destroy the design. Alternatively, ACF can provide a balance between freedom and the integrity of the design. You can build the theme using the Flexible Content Fields in ACF so that the client can move content around, add new content, and adjust settings, but all within the framework of the design.

But almost immediately, you’ll come across a downside of the Flexible Content Field: naming the modules. When you have a large site with multiple modules and module options, it can become increasingly frustrating for client to figure out and add in the module they’re looking for.

And so, we need a preview.

What We’re Creating: the Image Preview of the Module

The Flexible Content Field in ACF will automatically provide the setup in the backend when added to the page: (Of course these modules are ones I created for this site)

Add Module button pressed with hover above showing module options

What we’re creating will look like the image below. As you hover over the module, you’ll see an image preview of that module floating to the left.

Add a Module button with list of modules above and hover image to the left

Creating the Hover Preview using jQuery and CSS

There will be two parts to setting up the hover effect: Part 1 is the coding; Part 2 is adding the images. It’s surprisingly quick and easy to add this hover effect in!

Adding the jQuery and CSS to Functions.php

The first step is adding in the jQuery and CSS needed to the admin area of the site. To do this, add the following coding into the Functions.php file. You could also create a jQuery file and a CSS file and enqueue the file into functions.php, but to allow you to quickly see the effect we’re creating, you can add it into the functions.php file for now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* Hover Module Effect */
function my_acf_admin_head() {
$siteURL = get_site_url();
?>
<style type=”text/css”>
.imagePreview { position:absolute; right:100%; top:0px; z-index:999999; border:1px solid #f2f2f2; box-shadow:0px 0px 3px #b6b6b6; background-color:#fff; padding:20px;}
.imagePreview img { width:300px; height:auto; display:block; }
.acf-tooltip li:hover { background-color:#0074a9; }
</style>
<script>
jQuery(document).ready(function($) {
    $(‘a[data-name=add-layout]’).click(function(){
        waitForEl(‘.acf-tooltip li’, function() {
            $(‘.acf-tooltip li a’).hover(function(){
                imageTP = $(this).attr(‘data-layout’);
                $(‘.acf-tooltip’).append(‘<div class=”imagePreview”><img src=”<?php echo $siteURL; ?>/wp-content/themes/themeName/modules/images/’ + imageTP + ‘.jpg”></div>’);
            }, function(){
                $(‘.imagePreview’).remove();
            });
            });
        })
        var waitForEl = function(selector, callback) {
            if (jQuery(selector).length) {
                callback();
            } else {
                setTimeout(function() {
                waitForEl(selector, callback);
            }, 100);
        }
    };
})
</script>
<?php
}
add_action(‘acf/input/admin_head’, ‘my_acf_admin_head’);

The top section of styles deals with what we want the image preview to look like. I added a small box shadow to the image and padding so there’s a visual break with the content underneath the preview image.

Note that on Line 3 of the coding above we stored the site URL in a PHP variable. We’ll use this to help tell the site where to find the image in the jQuery section. If you’re building a site with a staging site that has a different site base file structure (ie. on localhost), this will make sure the image shows up on all your versions of the site.

The next section of jQuery makes the magic happen. When they hover over one of the options, we put the module name into a variable (imageTP) and find the image in the specified folder. You might add your module image previews in another folder, but I thought the best place for them was under the theme folder, in the folder containing all the coding for my modules, inside of an images folder there (/wp-content/themes/themeName/modules/images).

There’s one final tricky part of this setup: a function called “waitForEl”. The list of modules is being added dynamically to the page when you click on the “Add Module” button. And of course, in jQuery you can’t add an event (hover or otherwise) to an element that doesn’t exist. Instead, the function “waitForEl”, run when you click the “Add Module” button (line 12), calls a setTimeout Function (line 13) that tests every 100 milliseconds to see if whether the “.acf-tooltip li” class exists yet, and then calls the function to add an event (hover) to the element that now exists.

Here’s what happens:

Clicks “Add Module” -> Function waitForEl runs
[every 100 milliseconds, checks for the .acf-tooltip li element]

When the .acf-tooltip li element is found -> stops setTimeout & adds hover effect

User Hovers over .acf-tooltip li element -> Image Preview shows up

And finally, the last line of the coding above calls this function only in the ACF admin pages.

Adding the Preview Images

Add Module button pressed with hover above showing module options

Part two of this setup is saving and adding the preview images to the file structure. Again, I personally add them under my modules folder in the theme: /wp-content/themes/themeName/modules/images. Whatever setup you decide to go with, make sure you adjust the jQuery to reflect the new location.

Each image will need to be added into the file structure you chose, and they should be named as the modules are named. The easiest way to double-check the name is to hover over the module title, and right-click for “data-layout” attribute in the link:

1
<a href="#" data-layout="team_members" data-min="" data-max="">Team Members</a>

We’re utilizing this “data-layout” attribute to bring up the correct image. As you take screenshots (and resize and optimize!) of the modules on the front-end of the site, name them according to this “data-layout” attribute: team_members.jpg

And voila! You’ve got a beautiful new preview for your clients.

Do you have other techniques you use to make ACF easier or more intuitive for clients to use? Share below!

 

Join the Discussion

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>