Creating a Plugin for WordPress part 2. Creating a Custom Post type and Carbon Fields for the auto page.

The article explains how to create a custom post type for cars in WordPress and add fields using Carbon Fields. It covers setting up taxonomies and organizing custom fields for details like VIN, engine type, and price.

Creating a Plugin for WordPress part 2. Creating a Custom Post type and Carbon Fields for the auto page.

In this article, we’ll look at how to create a Custom Post Type, Custom Taxonomy, and fields for the Auto page.

Creating a Custom Post Type

Open the file AddCPT.php

The first thing we need to add to the init function

add_action( 'init', [ $this, 'register_post_auto' ] );

The first parameter is responsible for when our function will be called. Next, we need to pass an array of parameters, the first of which is $this is an instance of our class that indicates where to look for our function, followed by the name of the function.

Now we need to create a function that will register our new Auto post type. To do this, we need to call the register_post_type function and pass the following parameters to it

Here is our function code:

/**
 * Register CPT Auto.
 *
 * @return void
 */
public function register_post_auto(): void {
   register_post_type(
      'cars',
      [
         'label'         => null,
         'labels'        => [
            'name'               => __( 'Cars', 'ease-car-listing' ),
            'singular_name'      => __( 'Car', 'ease-car-listing' ),
            'add_new'            => __( 'Add car', 'ease-car-listing' ),
            'add_new_item'       => __( 'Adding a car', 'ease-car-listing' ),
            'edit_item'          => __( 'Car editing', 'ease-car-listing' ),
            'new_item'           => __( 'New car', 'ease-car-listing' ),
            'view_item'          => __( 'View car', 'ease-car-listing' ),
            'search_items'       => __( 'Search car', 'ease-car-listing' ),
            'not_found'          => __( 'Not found', 'ease-car-listing' ),
            'not_found_in_trash' => __( 'Not found in cart', 'ease-car-listing' ),
            'menu_name'          => __( 'Cars', 'ease-car-listing' ),
         ],
         'description'   => '',
         'public'        => true,
         'show_in_rest'  => null,
         'rest_base'     => null,
         'menu_position' => 30,
         'menu_icon'     => 'dashicons-car',
         'hierarchical'  => false,
         'supports'      => [ 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'revisions' ],
         'taxonomies'    => [ 'cars' ],
         'has_archive'   => true,
         'rewrite'       => true,
         'query_var'     => true,
      ]
   );
}

What parameters should you pay attention to?

menu_position – will allow us to change the position in the WordPress menu

menu_icon – Displays an icon
supports – The options we need [ ‘title’, ‘editor’, ‘thumbnail’, ‘excerpt’, ‘custom-fields’, ‘revisions’ ]

Creation of Custom Taxonomy

Adding another Action to the init function

add_action( 'init', [ $this, 'register_tax_mark' ] );

Everything is the same as with creating a new post type.
We create a function and call the function and call register_taxonomy in it and pass parameters to it

/**
 * Register taxonomy mark & models.
 *
 * @return void
 */
public function register_tax_mark(): void {
   register_taxonomy(
      'mark',
      [ 'cars' ],
      [
         'label'             => '',
         'labels'            => [
            'name'              => __( 'Marks & Models', 'ease-car-listing' ),
            'singular_name'     => __( 'Marks & Models', 'ease-car-listing' ),
            'search_items'      => __( 'Search Mark', 'ease-car-listing' ),
            'all_items'         => __( 'All Marks & Models', 'ease-car-listing' ),
            'view_item '        => __( 'View Marks', 'ease-car-listing' ),
            'parent_item'       => __( 'Marks', 'ease-car-listing' ),
            'parent_item_colon' => __( 'Models', 'ease-car-listing' ),
            'edit_item'         => __( 'Edit Marks & Models', 'ease-car-listing' ),
            'update_item'       => __( 'Update Marks & Models', 'ease-car-listing' ),
            'add_new_item'      => __( 'Add New', 'ease-car-listing' ),
            'new_item_name'     => __( 'New Marks & Models', 'ease-car-listing' ),
            'menu_name'         => __( 'Marks & Models', 'ease-car-listing' ),
            'back_to_items'     => __( '← Back to Marks & Models', 'ease-car-listing' ),
         ],
         'description'       => __( 'The main category is the brand of the car, the child category will be the model.', 'ease-car-listing' ),
         'public'            => true,
         'hierarchical'      => true,
         'rewrite'           => true,
         'capabilities'      => [],
         'show_admin_column' => true,
      ] );
}

We also need to add another taxonomy for engine size, for this, I register another function so it will be easier to disable or edit taxonomies in the future. And so we do everything by analogy

/**
 * Register taxonomy engine.
 *
 * @return void
 */
public function register_tax_engine(): void {
   register_taxonomy(
      'engine',
      [ 'cars' ],
      [
         'label'             => '',
         'labels'            => [
            'name'              => __( 'Engine', 'ease-car-listing' ),
            'singular_name'     => __( 'Engine', 'ease-car-listing' ),
            'search_items'      => __( 'Search Engine', 'ease-car-listing' ),
            'all_items'         => __( 'All Engine', 'ease-car-listing' ),
            'view_item '        => __( 'View Engine', 'ease-car-listing' ),
            'parent_item'       => __( 'Parens Engine', 'ease-car-listing' ),
            'parent_item_colon' => __( 'Parent', 'ease-car-listing' ),
            'edit_item'         => __( 'Edit Engine', 'ease-car-listing' ),
            'update_item'       => __( 'Update Engine', 'ease-car-listing' ),
            'add_new_item'      => __( 'Add New', 'ease-car-listing' ),
            'new_item_name'     => __( 'New Engine', 'ease-car-listing' ),
            'menu_name'         => __( 'Engine', 'ease-car-listing' ),
            'back_to_items'     => __( '← Back to Engine', 'ease-car-listing' ),
         ],
         'description'       => '',
         'public'            => true,
         'hierarchical'      => true,
         'rewrite'           => true,
         'capabilities'      => [],
         'show_admin_column' => true,
      ] );
}

Here is the complete code of our file https://gitlab.com/AlsconWeb/ease-car-listing/-/blob/25ed13774bf667a8cb553a947be13cda08a403c7/src/php/CPT/AddCPT.php

Creating fields for our auto post type

We move on to the CarboneFields.php file
First of all, we need to initialize the main Carbone Fields class so that it loads all of its classes.
Add action to the init function

add_action( 'after_setup_theme', [ $this, 'carbon_fields_init' ] );

We need to run this function on the after_setup_theme hook and all we need to do in this function is to run the static Boot function

Carbon_Fields::boot();

code of our function

/**
 * Carbone field init.
 *
 * @return void
 */
public function carbon_fields_init(): void {
   Carbon_Fields::boot();
}

Now we can add the fields themselves, for this, we need action from carbon fields
carbon_fields_register_fields

add_action( 'carbon_fields_register_fields', [ $this, 'add_carbone_fields_to_cars' ] );

We create this function and start creating fields.
To create the fields first of all we need to create a container like this.

Container::make( 'post_meta', __( 'Vehicle parameters', 'ease-car-listing' ) )

The first parameter specifies the type of container the second parameter is its name
Next, we need to specify where to display these fields, for this, we need to use the where function where the first parameter can be post_format, post_id, post_level, post_parent_id, post_ancestor_id, post_template, post_term, post_type
The second parameter is responsible for how to compare parameters and can take the value ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘IN’, ‘NOT IN’, ‘CUSTOM ‘ You can read more about these options in the documentation for CarboneFields.
In our case, we will use the post_type and give our title to our post.

->where( 'post_type', '=', 'cars' )

I will use tabs to separate the different types of fields so I will use the add_tab function which has 2 parameters. The first is the name of the tab that is displayed on the front and the second parameter is an array of carbon fields.

So, the complete code for this function came out like this. If you have any questions, you can always ask a question in the comments below the video.

/**
 * Add Carbon fields to CPT cars
 *
 * @return void
 */
public function add_carbone_fields_to_cars(): void {
   Container::make( 'post_meta', __( 'Vehicle parameters', 'ease-car-listing' ) )
            ->where( 'post_type', '=', 'cars' )
            ->add_tab(
               __( 'General', 'ease-car-listing' ),
               [
                  Field::make( 'text', 'ecl_vin', __( 'Vin number', 'ease-car-listing' ) )->set_width( 50 ),
                  Field::make( 'text', 'ecl_stock_number', __( 'Stock number', 'ease-car-listing' ) )
                       ->set_width( 50 ),
                  Field::make( 'text', 'ecl_car_mileage', __( 'Сar mileage', 'ease-car-listing' ) )
                       ->set_width( 50 ),
                  Field::make( 'select', 'ecl_body_type', __( 'Body type', 'ease-car-listing' ) )
                       ->add_options(
                          [
                             'compact'      => __( 'Compact', 'ease-car-listing' ),
                             'sedan'        => __( 'Sedan', 'ease-car-listing' ),
                             'coupe'        => __( 'Coupe', 'ease-car-listing' ),
                             'hatchback'    => __( 'Hatchback', 'ease-car-listing' ),
                             'kombi'        => __( 'Kombi', 'ease-car-listing' ),
                             'limousine'    => __( 'Limousine', 'ease-car-listing' ),
                             'microvan'     => __( 'Microvan', 'ease-car-listing' ),
                             'minivan'      => __( 'Minivan', 'ease-car-listing' ),
                             'pickup'       => __( 'Pickup', 'ease-car-listing' ),
                             'roadster'     => __( 'Roadster', 'ease-car-listing' ),
                             'suv'          => __( 'SUV', 'ease-car-listing' ),
                             'wagon'        => __( 'Wagon', 'ease-car-listing' ),
                             'pickup_track' => __( 'Pickup track', 'ease-car-listing' ),
                             'van'          => __( 'Van', 'ease-car-listing' ),
                          ]
                       )->set_width( 50 ),
                  Field::make( 'select', 'ecl_fuel_type', __( 'Fuel type', 'ease-car-listing' ) )
                       ->add_options(
                          [
                             'gas'      => __( 'Gas', 'ease-car-listing' ),
                             'diesel'   => __( 'Diesel', 'ease-car-listing' ),
                             'electric' => __( 'Electric', 'ease-car-listing' ),
                          ]
                       )->set_width( 50 ),
                  Field::make( 'text', 'ecl_car_year', __( 'Vehicle year', 'ease-car-listing' ) )
                       ->set_width( 50 ),
                  Field::make( 'text', 'ecl_horsepower', __( 'Vehicle Horsepower', 'ease-car-listing' ) )
                       ->set_width( 50 )
                       ->set_attribute( 'type', 'number' ),
                  Field::make( 'text', 'ecl_body_color', __( 'Body color', 'ease-car-listing' ) )
                       ->set_width( 50 ),
                  Field::make( 'text', 'ecl_interior_color', __( 'Interior color', 'ease-car-listing' ) )
                       ->set_width( 50 ),
                  Field::make( 'text', 'ecl_number_of_airbags', __( 'Number of airbags', 'ease-car-listing' ) )
                       ->set_width( 50 )
                       ->set_attribute( 'type', 'number' ),
                  Field::make( 'text', 'ecl_number_of_seats', __( 'Number of seats', 'ease-car-listing' ) )
                       ->set_width( 50 )
                       ->set_attribute( 'type', 'number' ),
                  Field::make( 'text', 'ecl_number_of_doors', __( 'Number of doors', 'ease-car-listing' ) )
                       ->set_width( 50 )
                       ->set_attribute( 'type', 'number' ),
               ]
            )
            ->add_tab(
               __( 'Additional Information', 'ease-car-listing' ),
               [
                  Field::make( 'rich_text', 'ecl_additional_info', __( 'Additional Information', 'ease-car-listing' ) ),
               ]
            )
            ->add_tab(
               __( 'Gallery', 'ease-car-listing' ),
               [
                  Field::make( 'media_gallery', 'ecl_gallery', __( 'Gallery', 'ease-car-listing' ) )
                       ->set_type(
                          [
                             'image',
                             'video',
                          ]
                       ),
               ]
            )
            ->add_tab(
               __( 'Price', 'ease-car-listing' ),
               [
                  Field::make( 'text', 'ecl_price', __( 'Price', 'ease-car-listing' ) )->set_width( 30 ),
                  Field::make( 'text', 'ecl_sales_price', __( 'Sales price', 'ease-car-listing' ) )
                       ->set_width( 30 ),
                  Field::make( 'text', 'ecl_by_month', __( 'Price per month', 'ease-car-listing' ) )
                       ->set_width( 30 ),
               ]
            );
}

You can always find the code of the entire file here: https://gitlab.com/AlsconWeb/ease-car-listing/-/commit/ee77a1cb22d2135f5bbeccdf26f9d37fb934b7c0#9ef8bcfcbdaab0d19e056f80ce0cc3d3c362c61f

Related posts

Insights and Tips from My Journey

JWT Token Authentication in WordPress REST API

JWT Token Authentication in WordPress REST API

  • 17.11.2024
  • 41
Effective Ways to Prevent Email Spam on Your Website

Effective Ways to Prevent Email Spam on Your Website

  • 10.11.2024
  • 67
How to Send Contact Form 7 Submissions to a Telegram Bot

How to Send Contact Form 7 Submissions to a Telegram Bot

  • 05.11.2024
  • 114
All Posts

Ready to Take Your Project
to the Next Level?

Let’s bring your vision to life with expert development and custom solutions.
Contact us now and get started on transforming your ideas into reality!