In WordPress.Using plug-ins Advanced Custom Fields (ACF)We can create a user-friendly product authentication query system. This article will detail how to implement this functionality using ACF and provide clear step-by-step instructions.
Step 1: Installation and Setup of ACF Plugin
- Installing the ACF Plug-in
- In the WordPress backend, click
plug-in (software component)
>Installation of new plug-ins
The - Search "Advanced Custom Fields", and then click
Installation
cap (a poem)start using
The - After installation, you will see in the backend navigation menu "Custom Fields" menu.
- In the WordPress backend, click
- Name the field group
- On the new page, the field group needs to be named. For example, it could be named "Product Certification Search"(ii) To facilitate identification and management.
- Selecting display rules for field groups
- Scroll down the page to find the Location(Location) Settings area. This is where the decision is made as to where these fields will be displayed.
- Set the condition as:
- Post Type (Article Type) Selection
Product
This means that these fields will only be displayed on the WooCommerce product edit page.
- Post Type (Article Type) Selection
- If you want the fields to be displayed on other page types, you can set them accordingly.
Adding Custom Fields. Now that we have created the field groups, we need to add specific custom fields next, such asCertification code, name and certification date of the productThe
- Add the first field - Authentication Code
- exist Field Label(field label) enter "Authentication Code", which will be the name of the field that the user will see in the backend.
- exist Field Name(field name) enter
certification_code
. This is a unique identifier for this field and will be used later in the code. - exist Field Type(Field Type), select
Text
, because authentication codes are usually composed of letters or numbers. - Other settings can be adjusted to suit your needs, such as setting it as a required field.
- Add a second field - Product Name
- strike (on the keyboard) Add Field(Add Field) to continue creating fields for the product name.
- exist Field Label Enter the "Product Name" in theField Name input
product_name
The - option Text as the field type, since the product name is also thecopiesFormat.
- Add a third field - Authentication Date
exist Field Type choose from Date Picker
, which allows the user to select the specific date of authentication via the date picker.
Step 2: Create an authentication query page
- Creating custom query pages
- switch to
web page
>New page
, create a new query page (for example:Product Certification Search
). - It is possible to use Elementor Pro or WordPress block editor to design the layout of the page.
- The page shouldInclude a formThe user can enter the product authentication code here to check the product information.
- switch to
- Add Product Certification Inquiry Form
Any form plugin can be used (e.g. Contact Form 7,WPForms) create the form and addinput fieldfor users to enter theirProduct Certification CodeThe
Set the form to send data to the custom query processing function you will use.
Step 3: Create Query Logic
- Writing Queries
- In the WordPress theme's
functions.php
file, a function needs to be written to process the query request entered by the user and return product authentication information based on the authentication code entered.
- In the WordPress theme's
function product_certification_lookup() {
if ( isset($_POST['certification_code']) ) {
$cert_code = sanitize_text_field($_POST['certification_code']);
// Query to retrieve product by certification code
$args = array(
'meta_query' => array(
array(
'key' => 'certification_code', // Custom field name
'value' => $cert_code, // Custom field name
'compare' => '='
)
)
);
$query = new WP_Query($args);
// Check if any products match the certification code
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$product_name = get_the_title();
$cert_date = get_field('certification_date'); // Retrieve the custom field
// Output the product name and certification date
echo 'Product Name: ' . $product_name . ' | Certification Date: ' . $cert_date.
}
} else {
// If no product is found, display a message
echo 'No matching product certification information found'; } else { // If no product is found, display a message.
}
// Reset post data
wp_reset_postdata(); wp_reset_postdata(); // Reset post data.
}
}
add_action('init', 'product_certification_lookup'); }
Description.
- This function handles the form submission where the user enters the product authentication code.
Used to search for WooCommerce products with matching authentication codes (stored in custom fields).
- If a match is found, the product name and certification date are displayed (also stored as custom fields).
- If no match is found, the message "No suitable match found for product certification information" is displayed.
- Associating forms with query logic
- On your query page, use theshort codeor PHP code to associate the query form with the authentication query function.
- When the user submits the authentication code, the function queries the product database based on the authentication code and returns the corresponding product information.
Insert the short code on the page, this will display a form on the page to enter the product authentication code and associate the data submitted by the form with the query function written.
[product_certification_form]
Step 4: Optimization and Style Adjustments
- Optimize user experience
- Additional feedback features can be added, such as prompting users when they submit an invalid authentication code "No relevant information found".
- Use CSS or Elementors' styling tools to adjust the presentation of query forms and results to ensure a user-friendly interface.
- Caching and Performance Optimization
- For sites with a large number of products, consider using caching or database indexing to improve query performance.
reach a verdict
By using the Advanced Custom Fields (ACF)With the help of WordPress, you can easily create a powerful product authentication lookup system for your WordPress website. This system enhances user trust and provides easy product authentication verification.