Wordpress custom fields: walkthrough, tips

WordPress custom fields (WP) are displayed in various shapes and sizes. Some include checkboxes and lists. Others may come with a date picker. There are those that accept only certain characters, for example, numbers. Like most WP features, add them to posts and pages through the plugin or manually without the plugin custom WordPress fields. In English literature, this term has the name "user fields".

About WordPress

WordPress (WP) is the best open source platform for quickly creating blogs. His popularity and strength is the Custom Fields community, which has been involved in the intensive development of the project and has been a leader in the CMS for the past 15 years.

WP is currently the most popular CMS in the world, which supports about 30% of websites. These statistics apply only to public sites. There are also a large number of unaccounted sites, for example, private sites, the number of which no one knows for sure. The result of a long history of brand development is opportunities for multimillion-dollar users with a base of 45,000 plugins and about 6,000 themes officially published on the WordPress website.

Another reason WP is interested in owners of news sites, hotel bookings, or e-commerce portfolios is WordPress custom fields and metadata architecture.

In WordPress, there are the main types of objects: post, user, comment, taxonomy, and media. They contain only the required fields provided by WP for their description. For example, a title for a product or message, a username, and a category name.





Custom metadata

Custom metadata




In the database, all this is expressed in the corresponding tables for WordPress objects and custom fields. You can interpret metadata as arbitrary (meta) attached to the primary data. They provide additional information and clarify them. They do not contain the meta itself.

For example, if you want to depict a “post about a product” object, you need additional information about price, quantity and quality. Thus, instead of adding columns to the table, the additional information data will be stored in metadata and then linked to Post. It becomes a product when metadata is used. If the user no longer wants it to be a “product”, you can associate it with arbitrary WordPress fields, for example, price and space, so Post becomes Room without changing its fundamental data.

This extremely interesting and flexible feature is the foundation for WordPress growth. In order for users to be able to add, delete or edit data, there must be a tool for manipulating them - these are arbitrary fields, a function that helps to create forms for interacting with metadata (MD).





Custom fields

If there is no arbitrary field interface (UI), the only way to add, delete or edit MDs is to use the code using the functions provided by the platform. In fact, the concept of MD is rarely mentioned because it is a technical term hidden in WP. And when it comes to custom fields, they usually realize that they include both the interface and the MD.

Basic types of objects




Custom fields create forms so that the user can add the necessary metadata. In the MD section, the main types of objects are Post, User, Comments, Media, and others. All these objects have their own custom fields, which can be seen when editing.

Usually there are many types of data that need to be added to each object. This can be a short text, image, or URL. Before displaying custom WordPress fields, users must select a field type.

  1. Text field, for example, name, address.
  2. Image field, for example, profile image, icon.
  3. File upload field - download a PDF file, download an invoice.
  4. Field editor - a brief description, introductory text.
  5. Relationships field - a field for links to other types of objects.
  6. Repeater field.
  7. URL field - website, social media profile.
  8. The selection field is an option from a predefined list.

Customization options

Customization options




In order to add such custom fields to WordPress, there are three options:

  • Use standard WP features by default
  • Use manual setting of WP function;
  • use plugins.

For some simple fields, such as text, just use the standard WordPress features. If you need to apply more complex fields, you can manually encode them. It is not too difficult for fields such as image and editor, but it is difficult for such as relationships.

The user can create websites in different industries using arbitrary fields. For instance:

  • travels - add descriptive fields for address, rating, price, guidebook;
  • hotel - add such fields as price, availability, capacity, services;
  • e-commerce - fields about price, volume, quantity.

In WP, you can say that where there are record types, there are custom fields.

The MDs and custom fields provided WP with a huge customization ability to meet the needs of 30% of websites in the world in all industries. When a user works with WP, even if he creates a simple blog, he cannot ignore arbitrary fields.

On the other hand, they are the cause of much controversy. Some people say that this makes the query slower, while others that it is not suitable for scaling. To find out the truth, there is no other way than to try and learn more about building WordPress and requesting data from a database of arbitrary fields.

Setting fields through the plugin

Setting fields through the plugin




There are many types of plugins that allow you to very quickly add custom fields to WP, but the favorite is optional custom fields.

Many people like this plugin because of its ease of use. In addition to simplicity, this plugin includes a WordPress custom field filter. Custom fields are created by him in an instant, in a few clicks.

First you need to install and activate the plugin. Open the WP control panel and click Plugins> Add New. Then they find "Advanced" in the search field and click "Install Now." Wait for the button "Activate" and click "Install."

Once the plugin is launched, you can see the new Custom Fields option in the control panel. Clicking on this button will open the page for groups of arbitrary fields, which should be empty at the time of the start of work. Click the "Add New" button at the top of the page.

The plugin will help you create a new group that contains several custom WP fields.

  1. First, click the "Add Field" button at the top of the page, choose what the label for this new group will be called. At this stage, they also choose which type of field you want to configure, for example, a field named E-mail.
  2. Now you can describe what this field is for, so that users of the site know what to do in this section. This condition is not necessary, but it is an example of good practice.
  3. Next, you must indicate whether this field is required or not. In this case, users can save messages or pages only after they are filled.
  4. There are other advanced settings that you can use.
  5. The editor is closed and the new field that was created appears in the list of groups.
  6. Include an option called Show This Field If, it can be used to configure display options.
  7. If you need to add more than one location to display a new custom WP field, then do this using the "Add Group Rule" button.
  8. Then click the “Publish” button and save the new custom field. At this stage, it should already work.
  9. Open the editor in one of the messages and check the end of the page.

Use the Advanced Custom Fields plugin to create the field, but not to display it on the site. By default, the field will be displayed only on the dashboard, and the entered information will be stored in the database. If you want the metadata to be visible to site visitors, you need to edit the files for the theme.

Manual setting

Manual setting




You can add WP fields manually and edit the theme to display them. The most difficult part is setting up the theme to display the metadata that is entered into these fields.

Algorithm settings.

  1. In order to add a custom WordPress field to one of the pages or posts, open the WP editor. Find the “Screen Settings” section at the top of the page, click and get access to the section where you can choose which WP fields will be displayed by default.
  2. Select an option called “Custom Fields”, after which a field will appear at the bottom of the screen.
  3. Adding a field is simple, just click the “Add” function and create a name for it.
  4. After entering the values, click "Add" at the bottom of the page to save them. You can create as many records as required. As with the WordPress custom field plugin, the entered data will be saved with the post.
  5. Users will not be able to view content in the public mode of the site. For this to happen, you need to report the theme by editing the WordPress loop function. This loop tells WP what data it should display when the user accesses the site. In most cases, this information includes the message heading or content and any other information.

Editing a topic loop

Editing a topic loop




To edit topics, you must access the site’s files using the File Transfer Protocol (FTP) or Filezilla, if you have not yet configured the FTP client.

After FTP is configured, go to the public_html / wp-content / themes directory. When opened, they find a folder that corresponds to the active topic on the site.

  1. Open the folder and find the single.php file. This file defines the individual display of the message and the contents of the loop. To change it, right-click on the file and select the "View / Edit" option, which will use the default text editor to open. Then find the loop inside the file.
  2. Now you need to insert several rows of data and create metadata.
  3. In this case, include a brief overview that will be displayed next to the email address and stored in a custom field. Thus, the display will be at the end of the article, but before the comments section. You can include code in various places so that metadata is displayed.

Built-in user data function

In order to add a new message or edit it, click “Screen Settings”.

Using the built-in function.

  1. Set the "Custom fields" checkbox.
  2. A field appears in the editor area. By default, this feature only works with messages. If you want it to support custom post types, declare it in the registration support options: 'support' => array ('title', 'editor', 'thumbnail', 'custom-fields').
  3. In the "Custom Fields" area, rows will appear to populate the data. This form is quite simple: an arbitrary name for a custom field is used as a key in the plain text of a custom field. Although this is a text box, it is quite flexible. It can save any data that can be converted to text: plain text, HTML, XML, SVG, JSON.
  4. Save the WordPress value of a custom field.
  5. Add additional data to a custom field, this function is also called clone fields in Meta Box or repeater fields in other plugins.

Self-processing a custom field from the user interface to save data is much more complicated.

Custom field custom processing




Frequently used functions show a list of fields in the external interface in an ordered list, provide a custom value for fields in a key / value array, return an array of keys for all fields, an array of all values ​​for a custom field $ key.

Custom Listing

If you want the output of custom WordPress fields to be displayed immediately after the publication header both in the archive and on a separate page, add the_meta () function to the template-params / post / content.php file.

Custom Listing




Custom fields are a way for WP to store arbitrary additional data for the content, such as the name of the author, date of publication of the book. To make custom fields flexible and compatible with various types of data, WP develops meta-tables (meta-post, meta-term, meta-user and meta-comment) in the form of "key-value".

In accordance with this, each custom field is saved as one row in the database. This approach allows developers to store an unlimited amount of information regardless of their structure. But the disadvantage is the rapid spread of the database. Because the number of custom fields is usually very large. The WordPress project has all the solutions to optimize the storage of custom fields in the database in order to improve the site performance. There are some drawbacks to using the default WordPress database structure for meta tables.

Meanwhile, the database structure will prevent the creation of complex queries. Thus, to solve this problem for arbitrary fields, you need to change the storage method. As already noted, custom fields are not recommended for search or query, although in practice such operations sometimes have to be performed. Thus, the storage optimization for custom fields should be divided into two tasks: optimization of the storage used only for display / storage, as well as data request.

Custom tables

To completely solve this problem, you need to save data not as WordPress offers by default, but in the form of user tables. Using them, you can specify the number, data type, and storage method.

This data structure is very understandable, but can be a big problem for many WordPress developers. To simplify the work for Meta Box users, you can use the MB Custom Table extension to create tables for processing data about retrieval / storage from arbitrary fields and providing a cache class to improve performance.

Advantages of creating tables:

  • clear the data structure;
  • perform a query on custom fields and create complex queries;
  • custom fields are saved on one line.

Minuses:

  • Do not use the built-in WordPress metadata API functions
  • data security;
  • implement a cache layer when data grows independently.

Areas of use

In any case, the information that is added when using the field can be used for different purposes. However, in most of them these fields are used for “metadata”. For example, in a WP post, a headline is created to describe the content, and technically, this is metadata.

Other types of metadata include message tags, meta descriptions, page categories, and more if the user has something in common, all that WordPress provides to add content information. In some cases, metadata needs to be added, but WP does not offer fields designed for this. To do this, you will need to add custom WordPress fields and associate them with content.

With the right tools and a step-by-step helper, you can create almost all kinds of custom WP fields. An example is the addition of a star rating system to posts. By creating custom fields for a star rating system, you can move posts by popularity or ask visitors to leave reviews, although this involves more complex settings.

It's important to note that custom WordPress custom fields in posts and custom post types are not the same thing. A custom WordPress post is a format specific to the type of content, for example post reviews or product pages that should categorize posts. But, of course, on the same page or message there is usually a field and the type of custom message, and these two elements can be applied in isolation.

As data grows, performance and scalability become big problems for all websites. It is important to consider optimizing storage for arbitrary fields in the database. Using the techniques described above, you can save space for storing the database and at the same time improve query performance.




All Articles