When we are creating a form in BuddyForms, whose purpose is to create or edit entries of a Post Type, sometimes we run into the situation that the post has one or more metadata. This metadata could be related to some post functionality that we also want to integrate in our form.
In most cases, these functionalities are implemented through themes or plugins, which use metadata as persistent variables to stock information related to the process or processes involved.
It’s important to mention that not all the metadata can be successfully integrated in BuddyForms. Depending on how these are being stocked in the database, we will know whether or not we can integrate them into our form. In general, if a metadata is stored as a serialized text string, it cannot be embedded, since each metadata must be associated with an input in the BuddyForms form.
What is Metadata?
The metadata is information associated to a post, users, comments and terms, which are generally used to constitute core functionalities, themes or plugins related to that type of data. This information can usually be modifiable from the user interface in the WordPress Administrator, for example, when creating or editing the entry of a publication in the Administrator, this information is usually introduced as a set of options, also called meta- box, which can be viewed or changed at any time by the user.
Note: A post Metadata is stored as an entry in the wp_postmeta table in the WordPress database
Use cases
Below we will present a series of use cases, using popular plugins as a reference, where it might be useful to integrate metadata with a BuddyForms form.
Note: The following examples may vary depending on the time and version of the plugin or related theme, they’re also practical examples that may not be applicable in certain contexts. Relevant research and testing is recommended to determine whether a use case is applicable to you or not.
WooCommerce
Let’s suppose we want to create a BuddyForms form as simple as possible, which is able to edit or create functional WooCommerce products from the Front, with a series of predefined configurations in each product, having only the fields: Title, Content, Featured Image and Price (see Image 1).
The solution
It’s important to mention that besides BuddyForms, we need to have the BF WooCommerce Form Elements add-on installed to be able to carry out the example.
The first thing we’ll do is create a BuddyForms form with the required fields (see Image 1), including the WooCommerce fields (special field of BF WooCommerce Form Elements with the product options) and a price type field, which will be used to establish the product price. Finally we should have something similar to this:
The second thing we’ll do is to configure the WooCommerce field, so all the available options are hidden, in this way our form will be shown as in Image 1. The advantage of adding a WooCommerce type field with all the hidden options is that it allows us to predefine the setting that each product will have once it has been sent, without the need to “overload” the form with all the WooCommerce options.
As we need users to be able to set each product price created, we must configure the price type field (Regular Price) so when the product is sent, the metadata associated with the Regular Price field of that particular product is overwritten.
Note: WooCommerce uses Product as Post Type, which in conjunction with several metadata and taxonomies, defines the information for each product created.
The way to configure the Regular Price field or any other field that pretends to overwrite a metadata is very simple, we just have to set the field’s slug with a value that matches the metadata keyword (meta_key) that we want to overwrite.
Note: A way to obtain the meta_key of a metadata is by searching in the wp_postmeta table, if we know a post’s ID, we can perform a simple query to filter only the records associated with that Post. Another option is to search the documentation for the theme or plugin associated with the Post Type, generally this information is found in the section for developers. As a last option we could carry out an exhaustive inspection in the code, almost always this turns out to be the infallible option, but also the most laborious one.
Once the appropriate slug has been established for our price type field and our WooCommerce field configured, we can create or edit products from our BuddyForms form, with the price and other options specified in there.
Note: When adding new fields to overwrite a Post metadata, we must choose a field type that is consistent with the original field type used in the metadata displayed on the administrator page destined to create or edit an entry for that Post.
Events Calendar
Let’s suppose we want to create a BuddyForms form that allows us to create and edit events from the Front. Our form must have the fields: Title, Content, Start Date, End Record, Event URL. See Image 3.
The solution
As in the previous example, the first thing we must do is create a BuddyForms form, which contains the necessary fields shown in Image 3. After this we must identify and set the fields that will be used to be integrated into the metadata, in our case, they will be: Start Date, End Date and Event URL.
To configure these fields we must first identify the metadata that we want to integrate, besides that, we must determine if these are compatible with the integration. Then we must configure the slug of each field according to the meta_key of the corresponding metadata (see Image 4 and 5).
In the case of the Start Date and End Date fields, besides slug we must also configure the date format, so it matches the format used by the plugin when storing the information. The easiest way to identify the correct format is to inspect the wp_postmeta table for records that match the meta_key _EventEndDate or _EventStartDate. In this case, the date formats must be configured in the next way:
Note: As we mentioned above, these results may vary with time and version of the plugin, so they should be taken only as illustrative examples.
Once we have established the appropriate slugs and we have configured the date format for the Start Date and End Date fields, we’ll be ready to start creating or editing entries, if we need to add more fields, just repeat the aforementioned procedure.
Conclusion
As we have already seen, sometimes it’s beneficial to integrate the metadata of a Post Type in our BuddyForms form, we also saw how important it’s to know how the metadata is being stored in the database, to determine whether or not it’s embeddable in a BuddyForms form.
It’s important to note that each integration must be preceded by a corresponding investigation, since not in all cases an integration is applicable, sometimes a custom implementation may be necessary to complement the integration.
ThemeKraft offers the opportunity to create content for us. Guest Post
Mechatronics Engineering student, self-taught in several areas related to technology, design and development. Enthusiast of Audiovisual Programming and Design, with great Scientific and Humanistic vocation.
0 Comments