Custom Fields
Re-Envisioned
Support MB Custom Table How to Install Custom Table if I created the table with phpmyadmin

  • Creator
    Topic
  • #29940

    I read over the docs, but, I can't get it to work.

    First of all, I have WordPress 5.8 and I installed the latest WooCommerce and I installed Metabox from the WordPress plugins directory and I installed MB Custom Table.

    So, I already created a table with 7 columns with phpmyadmin.

    One of the columns is ID, it's unsigned and does not allow nulls.

    Now, I don't understand what to do next.

    I saw the php file in your docs:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array(
            'title'        => 'Meta Box Title',
            'storage_type' => 'custom_table',    // Important
            'table'        => 'my_custom_table', // Your custom table name
            'fields'       => array(
                array(
                    'id'   => 'address',
                    'type' => 'text',
                    'name' => 'Address',
                ),
                array(
                    'id'   => 'phone',
                    'type' => 'text',
                    'name' => 'Phone',
                ),
                array(
                    'id'   => 'email',
                    'type' => 'email',
                    'name' => 'Email',
                ),
            ),
        );
        return $meta_boxes;
    }
    

    I changed my_custom_table to my table name.

    I changed the 'id' to match the column names in my custom table. And then I changed the types and names accordingly.

    Do I need to change 'your_prefix_register_meta_boxes'?

    Then, where do I put this code? Is it a new file? I think in the video you just put it in a file called test.php in the mu-plugins directory. Is that all I have to do?

    I did that and when I went to my site (front end), it displayed the code on the site.

    Then, I tried adding it to my functions.php file in the child theme directory and I deleted the file from the mu-plugins directory. The code doesn't display on the front end anymore, but, nothing happens either when I edit a product.

    BTW, I want to add and save custom fields for my WooCommerce Products. So, please let me know if this even works for that.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #29948

    I guess I should have checked if it worked with a post - which it did.

    So, I guess it's not supposed to work with Products? Any chance it's in the pipeline? Or maybe I'm supposed to add another extension? I didn't see one with a name that would suggest that.

    #29971
    Long Nguyen
    Moderator

    Hi,

    Yes, it works with any post type including the Product. You need to assign product to the setting post_types when registering the meta box.

    add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
    function prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = [
            'title'      => 'Personal Information',
            'post_types' => 'product',
    ...

    Get more details here https://docs.metabox.io/creating-meta-boxes/#using-code

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.