How to add Custom Fields
Along with the Inventory section, the theme comes with various other sections (like Testimonials, Teams, FAQs, etc.) in the admin panel. All these sections contain custom fields to manage various data.
If you have a custom requirement and want to add the custom field(s) in inventory or other sections, you can use WordPress’s filter hooks to extend the existing fieldsets. All the fieldsets for all different sections contain a filter hook so that users can extend them by hooking a custom function.
You can find the filter hooks for each field-sets in their relevant code files, and code files are located at the below path.
WORDPRESS_ROOT\wp-content\plugins\cardealer-helper-library\includes\acf\fields
So, for example, if you want to add custom fields in the Inventory items, you will have to hook your custom function (with new fields definition/parameters) to the “cardealer_acf_car_data” filter tag, which is available in the “car-data.php” file (path of this file is given below).
WORDPRESS_ROOT\wp-content\plugins\cardealer-helper-library\includes\acf\fields\car-data.php
Advanced Custom Fields Pro (ACF)
We are using Advanced Custom Fields Pro (ACF) plugin to add fields in the backend. So, you can add new fields by registering new fieldsets using the ACF API. Please refer to the below documents for detailed information about registering new field(s) and available parameters for different field types.
- https://www.advancedcustomfields.com/resources/register-fields-via-php/
- https://www.advancedcustomfields.com/resources/#field-types
Here is the sample code, in which we hooked a custom function to the “cardealer_acf_car_data” filter tag and added two “text” fields in the inventory items. Note that the below-provided code is just a sample code; you can customize it as per your requirements to add more fields and details. To add more field types, please refer to the ACF documentation links provided above.
function cardealer_child_extend_car_data_fieldset( $fieldset ){ // Get old fields $old_fields = $fieldset['fields']; // prepare new fields $new_fields = array( /*-------------------------------------------------------------------- * ADD YOUR NEW FIELD CODE INFORMATION BELOW * * DO NOT TOUCH CODE ABOVE *--------------------------------------------------------------------*/ // Add Tab // This is must required to separate newly added fields from existing fields. array ( 'key' => 'field_3a55ba3715a94c809a00866c8c55816a', 'label' => esc_html__('Custom Fields','theme-textdomain'), 'name' => '', 'type' => 'tab', 'instructions' => '', 'placement' => 'left', 'endpoint' => 0, ), // Minimal Field Parameters array ( // The value of the "key" parameter must be unique key containing alphanumeric characters. Do not leave it blank. // Use "https://www.uuidgenerator.net/version4" to generate unique key. 'key' => 'field_0d85650130514845b98b425f8ae34aa0', 'label' => esc_html__( 'Custom Field 1', 'theme-textdomain' ), 'name' => 'custom_field_name_1', 'type' => 'text', ), // Full Field Parameters // For detailed information about available parameters of different field types, please refer ACF's field type docuements. // https://www.advancedcustomfields.com/resources/#field-types. array ( // The value of the "key" parameter must be unique key containing alphanumeric characters. Do not leave it blank. // Use "https://www.uuidgenerator.net/version4" to generate unique key. 'key' => 'field_770597ed1dbb4eba84294a63542da105', 'label' => esc_html__( 'Custom Field 2', 'theme-textdomain' ), 'name' => 'custom_field_name_2', 'type' => 'text', 'prefix' => '', 'instructions' => '', 'required' => 0, 'conditional_logic'=> 0, 'wrapper' => array ( 'width'=> '', 'class'=> '', 'id' => '', ), 'default_value'=> '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'maxlength' => '', 'readonly' => 0, 'disabled' => 0, ), /*-------------------------------------------------------------------- * ADD YOUR NEW FIELD CODE INFORMATION ABOVE * * DO NOT TOUCH CODE BELOW *--------------------------------------------------------------------*/ ); $merged_fields = array_merge( $old_fields, $new_fields ); // Merge old and new fields. $fieldset['fields'] = $merged_fields; // Assign fields back to field-set. return $fieldset; // Return field-set. } add_filter( 'cardealer_acf_car_data', 'cardealer_child_extend_car_data_fieldset' );
Display Above Added Field Data on the Vehicle Detail Page
The above code will display the newly added field in the admin panel. If you want to display the newly added field(s) data on the vehicle details page on the front side, you will have to add field(s) data by hooking custom function to the “cardealer_car_attributes” filter hook.
Here is the sample code to display the above-added field’s data on the vehicle details page. You can add the below sample code in the child theme’s functions.php
. And, in child-theme, the path of the functions.php
should look like the path given below.
WORDPRESS_ROOT\wp-content\themes\cardealer-child\functions.php
function cardealer_child_extend_details_on_vehicle_detail_page( $attributes, $post_id ){ // Debug This // Set "$debug_this" true, to display full list of available attribute keys (including attributes/fields). $debug_this = false; if( $debug_this ){ echo '<pre>'; print_r($attributes); echo '</pre>'; exit; } // You can add new attributes like this /* $attributes[ 'attribute_key' ] = array( 'attr' => esc_html__( 'Attribute Name', 'theme-textdomain' ), 'value' => esc_html__( 'Attribute Value', 'theme-textdomain' ), ); */ // Here is the detai of parameters in above code. // attribute_key = Unique Attribute Key in small letters, without space and spacial character. // attr = Attribute Name // value = Attribute Value $custom_field_name_1 = get_field( 'custom_field_name_1', $post_id ); if ( $custom_field_name_1 && ! empty( $custom_field_name_1 ) ) { $attributes[ $custom_field_name_1 ] = array( 'attr' => esc_html__( 'Custom Field 1', 'theme-textdomain' ), 'value' => esc_html( $custom_field_name_1 ), ); } $custom_field_name_2 = get_field( 'custom_field_name_2', $post_id ); if ( $custom_field_name_2 && ! empty( $custom_field_name_2 ) ) { $attributes[ $custom_field_name_2 ] = array( 'attr' => esc_html__( 'Custom Field 2', 'theme-textdomain' ), 'value' => esc_html( $custom_field_name_2 ), ); } /*-------------------------------------------------------------------- * ADD YOUR CODE ABOVE * * DO NOT TOUCH CODE BELOW *--------------------------------------------------------------------*/ return $attributes; } add_filter( 'cardealer_car_attributes', 'cardealer_child_extend_details_on_vehicle_detail_page', 10, 2 );
Important Notes:
- If an issue occurs while adding custom fields, you will have to solve it yourself.
- We assume that you are using the child theme for the customizations and adding the above code in the child theme’s
functions.php
. You can find the child theme in the theme package downloaded from Themeforest.