Blog

WordPress Development: What Are Custom Fields and How Do You Use Them?

Written by Pressable on
WordPress Custom Fields

WordPress has many advanced features for customization, one of which is the ability to create custom fields. Whether you’re building a blog, a local business website, an e-commerce store, or any other WordPress-powered site, you may want to use custom fields to streamline your customization options.

What Are Custom Fields?

Custom fields are pieces of custom metadata. You can add them to posts and pages. As metadata, custom fields provide information about a post or page’s content. After adding a custom field to a post or page, you can edit your theme to automatically display it.

Metadata is a core component of WordPress. Even without custom fields, WordPress will use metadata to categorize and customize posts and pages. Author names and publication dates, for instance, are metadata. Custom fields are used for similar purposes. With custom fields, you can categorize and customize posts and pages by adding metadata to them.

Common uses for custom fields include:

  • Disclosure statements
  • Author bylines
  • Video information
  • Product information
  • Business information
  • Real estate listings
  • Reviews
  • Customer testimonials

How Custom Fields Work

Each custom field consists of a key and value. The key represents the name of the custom field, whereas the value represents the definition. Keys are typically hidden; they are simply used to identify their corresponding values, which are the actual pieces of metadata

Keys are reusable. When you create a key for a post or page, it will be automatically saved. You can then choose this key from a drop-down list to reuse it. If you want to create another custom field for the same post or page, for instance, you can reuse the key by choosing it from the drop-down list. While keys are reusable, though, values are not. You’ll have to create each value by hand.

Custom fields work in conjunction with Hypertext Preprocessor (PHP). Custom fields alone won’t change the appearance of a post or page’s content. They are simply pieces of metadata, so visitors won’t see them without a little extra work on your behalf. To show custom fields, you’ll need to use PHP.

You can add PHP code to your theme to display custom fields. WordPress support several PHP queries for custom fields. PHP queries are designed to retrieve custom fields and display them in a given area of your theme.

If you regularly publish videos, you may want to use custom fields to provide visitors with information about the videos. You can create custom fields for video length and source. When visitors land on posts and pages with videos, they’ll see how long the videos are and the source from which you obtained them.

Custom fields offer an easy way to add disclosure statements to posts and pages as well. If you’ve been paid to write a review about a product, you may want to add a disclosure statement to the post or page. For a single review, you can manually add the disclosure statement. For multiple reviews, you may want to use custom fields. With custom fields, you can specify which posts and pages contain a sponsored review. PHP will allow you to automatically display the disclosure statement on those posts and pages.

How to Use Custom Fields

The option to create custom fields in the Gutenberg editor is disabled by default. To enable it, open a post or page and click the three-dot button in the top-right corner, followed by “Preferences.” Under the “Panels” section is the option for custom fields. Assuming it’s toggled to the off position, clicking it will enable custom fields on your website.

If you use the old Classic editor, you won’t need to enable custom fields. This feature is enabled by default with the Classic editor. Whether you use the Gutenberg or Classic editor, you should see the custom fields tool on posts and pages. It’s found directly below the main editor interface.

The custom fields tool features a name section and a value section. To create a new custom field, click “Enter new” under the name section. You can then enter a name or key for the custom field in the name section.

Along with a name, you’ll need to enter a value. The value is the metadata that, when retrieved, will be displayed on your website. Keep in mind that you can create multiple custom fields with the same name but different values. Regardless, each custom field requires a name and value.

You can use PHP to display custom fields on your website. When you add a custom field to a post or page, WordPress will save it in your website’s database. Like with other database-stored content, a PHP query is needed to retrieve the custom field. Adding the appropriate PHP query to your theme will retrieve and display custom fields in that area.

The “get_post_custom_values” PHP query will retrieve the values of custom fields. You can specify a particular custom field with the “key” parameter. The values from the specified field will be retrieved and displayed. Check out the support article at wordpress.org/support/article/custom-fields for a list of supported PHP queries.

Consider Custom Fields Plugins

If you’re struggling to create custom fields, you may want to use a plugin. Advanced Custom Fields is a popular custom fields plugin for WordPress. Available in free and paid versions, it offers a beginner-friendly builder for custom fields.

With Advanced Custom Fields, you’ll have dozens of field types from which to choose. You can use them to easily add metadata to posts and pages. Some of the field types included in Advanced Custom Fields are text, range, checkbox, true or false, select and time picker, and Google Maps. You can also load the metadata in your website’s theme without messing with PHP.

Conclusion

WordPress supports custom metadata in the form of custom fields. Consisting of a key and value pair, each custom field consists of information about a post or page. If you’re comfortable with PHP, you can create them by hand. If you have little or no experience with PHP, you can use a plugin like Advanced Custom Fields.

Web Performance

Load time matters! Do you know how fast your site is?

LEARN MORE

Pressable

Pressable's managed WordPress hosting allows you to run your websites or your clients' websites on the powerful WP Cloud: the only cloud platform built exclusively for optimized WordPress site speed, uptime, and scalability.