dynamically add a custom field or metabox to custom post type

I am creating a plugin that registers a Custom Post Type. Let’s call it “Cars”. I am looking to be able to add custom fields(or metaboxes) when I am editing a car, just like what you can do now with a custom field in a post or a CPT but I want to be able to control the HTML/CSS for it(for example I would like it to have 3 input fields).
So for example when you are editing a car, there would be a “add new field” button and when you press it a new custom field/metabox(with predefined structure and styling by me) would be added.

Let me know if I am not clear. Thank you!

Related posts

Leave a Reply

1 comment

  1. To add a metabox to a post type car use the proper hook:

    add_action( 'add_meta_boxes_car', 'register_car_metabox' );
    
    function register_car_metabox()
    {
    
        add_meta_box(
            'car-data',
            'Car data',
            'car_metabox_callback',
            NULL,
            'normal',
            'default'
        );
    }
    

    The content will be created in your callback:

    function car_metabox_callback()
    {
        # get post meta and print input fields
    }