A Beginner’s Guide to Advanced Custom Fields
WordPress is an open-source content management system (CMS). This means that it gives you the flexibility to customise your website how you want. You have a lot of power at your figure tips with the use of themes, plugins and custom code. With these components, you can customise as little or as much as you want. In this blog, we will discuss how and why you should use the Advanced Custom Fields plugin on your WordPress website.
What Are Advanced Custom Fields (ACF)
Advanced Custom Fields (ACF) is a WordPress plugin, that is used for over 1 million websites. It is designed to make it simple and easy when it comes to editing complicated content. ACF allows you to add fields in addition to the WordPress visual editor or choose to hide it completely. Using ACF you have a wide range of field types to choose from. This including text, media, layout, and much more.
An Example Situation
On the homepage of your site, you want a full-width image with text over it (the hero image). However, your client wants to change both the text and the image regularly. Knowing your client’s ability there are 3 things you need to look out for:
- An easy way to swap out the image with a new one.
- The ability to edit the text over the image.
- To avoid anything that could break the format of the website.
The Issue With WordPress Visual Editor
The full-width image with text is possible in WordPress Visual Editor you will just need to add some CSS classes to the content and then coded it the way you want in the front end.
The problem with this method is that the visual editor is very sensitive when it comes to CSS classes. What I mean by this is if you hit the backspace button one too many times, when trying to replace the text, you can accidentally delete the class. Therefore, the removal of the class removes the code for this content on the front end.
This approach isn’t great for people who don’t have much knowledge of WordPress and CSS. Hence this is not appropriate for our example situation.
The ACF Approach
ACF allows you to create specific fields that our client can use easily and safely to edit the image and text, without the fear of deleting code by accident. Using this approach:
- Provides a simple editing area for the individual content element.
- Allows you to control the file size, type and dimensions of the image to upload, this will avoid “client mistakes”.
- Edit our theme template’s HTML to place our image and text exactly the way we want.
The use of these features enables you to carefully-craft your layout whilst providing your client with the freedom to make changes.
To create our image and text the way we want on the front end we will need to add some code. The code we’ll use for this example situation is:
<h2><?php the_field('sub_title'); ?></h2>
<h1><?php the_field('title'); ?></h1>
$image = get_field('background');
background-image: url(<?php echo $image['url']; ?>);
<?php endif; ?>
This code needs to be added in the pages PHP. Then we will combine this code with some CSS to get it looking exactly how you want.
In both options you need CSS but the ACF approach prevents accidental mistakes. The benefit of using ACF is once it is setup how you want; the layout will stay like that. This means that by using this method your client doesn’t have to remember anything about editing the home page. Although there are some problems for the WordPress Visual Editor it can still work well with the ACF if it suites the circumstances.