Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Step 1: Register a model.
- add_action( 'init', 'prefix_register_transaction_model' );
- function prefix_register_transaction_model() {
- mb_register_model( 'transaction', [
- 'table' => 'transactions',
- 'labels' => [
- 'name' => 'Transactions',
- 'singular_name' => 'Transaction',
- ],
- 'menu_icon' => 'dashicons-money-alt',
- ] );
- }
- // Step 2: Create a custom table for the model.
- add_action( 'init', 'prefix_register_transaction_table' );
- function prefix_register_transaction_table() {
- MetaBox\CustomTable\API::create(
- 'transactions', // Table name.
- [ // Table columns (without ID).
- 'created_at' => 'DATETIME',
- 'amount' => 'BIGINT',
- 'email' => 'VARCHAR(20)',
- 'gateway' => 'TEXT',
- 'status' => 'VARCHAR(20)',
- 'screenshot' => 'TEXT',
- ],
- ['email', 'status'], // List of index keys.
- true // THIS: Must be true for models.
- );
- }
- // Step 3: Register fields for model, corresponding to the custom table structure.
- add_filter( 'rwmb_meta_boxes', 'prefix_register_transaction_fields' );
- function prefix_register_transaction_fields( $meta_boxes ) {
- $meta_boxes[] = [
- 'title' => 'Transaction Details',
- 'models' => ['transaction'], // Model name
- 'storage_type' => 'custom_table', // Must be 'custom_table'
- 'table' => 'transactions', // Custom table name
- 'fields' => [
- [
- 'id' => 'created_at',
- 'type' => 'datetime',
- 'name' => 'Created at',
- 'js_options' => [
- 'timeFormat' => 'HH:mm:ss',
- ],
- 'admin_columns' => true,
- ],
- [
- 'id' => 'amount',
- 'type' => 'number',
- 'name' => 'Amount',
- 'append' => 'USD',
- 'admin_columns' => [
- 'position' => 'after id',
- 'sort' => true,
- ],
- ],
- [
- 'id' => 'gateway',
- 'name' => 'Gateway',
- 'admin_columns' => true,
- ],
- [
- 'id' => 'status',
- 'type' => 'select',
- 'name' => 'Status',
- 'options' => [
- 'pending' => 'Pending',
- 'completed' => 'Completed',
- 'refunded' => 'Refunded',
- ],
- 'admin_columns' => true,
- ],
- ],
- ];
- $meta_boxes[] = [
- 'title' => 'Additional Transaction Details',
- 'models' => ['transaction'], // Model name
- 'storage_type' => 'custom_table', // Must be 'custom_table'
- 'table' => 'transactions', // Custom table name
- 'fields' => [
- [
- 'id' => 'email',
- 'type' => 'email',
- 'name' => 'Email',
- 'admin_columns' => [
- 'position' => 'after amount',
- 'searchable' => true,
- ],
- ],
- [
- 'id' => 'screenshot',
- 'type' => 'image_advanced',
- 'name' => 'Screenshots',
- 'admin_columns' => true,
- ],
- ],
- ];
- return $meta_boxes;
- }
- add_action( 'init', function() {
- // Get raw value via API.
- $value = \MetaBox\CustomTable\API::get_value( 'screenshot', 17, 'transactions' );
- // ray( $value );
- // Get meaningful value with helper function.
- $value2 = rwmb_meta( 'screenshot', [
- 'object_type' => 'model',
- 'type' => 'transaction',
- ], 17 );
- // ray( $value2 );
- }, 30 ); // Priority 30 to ensure meta boxes are registered (at init with priority = 20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement