Public Ticket #3848015

How to code display add field in 3 column.

Closed
Comments
18 Tickets 3
Hung Tao started the conversation 2 years ago on March 31, 2025 at 4:28am

Link : https://fujikyo.com.vn/wp-login.php?loggedout=true&wp_lang=vi

user : Admin An

Pass : 11111222223344zzxxdfg

Link : https://fujikyo.com.vn/wp-admin/edit.php?post_type=b2bking_conversation

List Conversation donot not show phone  and campany. ( image_table_show_data.png )

Link : https://fujikyo.com.vn/wp-admin/edit.php?post_type=b2bking_quote_field

This is code:


// Add custom meta box to b2bking_conversation post type
function add_b2bking_custom_fields_meta_box() {
    add_meta_box(
        'b2bking_conversation_fields', // Meta box ID
        'Conversation Details', // Meta box Title
        'render_b2bking_custom_fields_meta_box', // Callback function
        'b2bking_conversation', // Post type
        'normal', // Context
        'high' // Priority
    );
}
add_action('add_meta_boxes', 'add_b2bking_custom_fields_meta_box');

// Render Meta Box content
function render_b2bking_custom_fields_meta_box($post) {
    // Add nonce for security
    wp_nonce_field('b2bking_custom_fields_nonce', 'b2bking_custom_fields_nonce');

    // Get existing values
    $company_name = get_post_meta($post->ID, 'b2bking_custom_field_2224', true);
    $customer_email = get_post_meta($post->ID, 'b2bking_request_custom_quote_email', true);
    $customer_phone = get_post_meta($post->ID, 'b2bking_custom_field_2220', true);
    ?>
    <div style="margin: 20px 0;">
        <p>
            <label style="display: block; margin-bottom: 5px;"><strong>Company Name:</strong></label>
            <input type="text"
                   name="b2bking_custom_field_2224"
                   value="<?php echo esc_attr($company_name); ?>"
                   style="width: 100%;">

        </p>
        <p>
            <label style="display: block; margin-bottom: 5px;"><strong>Customer Email:</strong></label>
            <input type="email"
                   name="b2bking_request_custom_quote_email"
                   value="<?php echo esc_attr($customer_email); ?>"
                   style="width: 100%;">
        </p>
        <p>
            <label style="display: block; margin-bottom: 5px;"><strong>Customer Phone:</strong></label>
            <input type="text"
                   name="b2bking_custom_field_2220"
                   value="<?php echo esc_attr($customer_phone); ?>"
                   style="width: 100%;">
        </p>
    </div>
    <?php
}

// Save Meta Box data
function save_b2bking_custom_fields_meta($post_id) {
    // Check if nonce is set and valid
    if (!isset($_POST['b2bking_custom_fields_nonce']) ||
        !wp_verify_nonce($_POST['b2bking_custom_fields_nonce'], 'b2bking_custom_fields_nonce')) {
        return;
    }

    // Check if autosaving
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    // Check user permissions
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }

    // Check if correct post type
    if (get_post_type($post_id) !== 'b2bking_conversation') {
        return;
    }

    // Save company name
    if (isset($_POST['b2bking_custom_field_653732'])) {
        $company_name = sanitize_text_field($_POST['b2bking_custom_field_2224']);
        update_post_meta($post_id, 'b2bking_custom_field_2224', $company_name);
       // update_post_meta($post_id, '_b2bking_custom_field_653732', $company_name);
    }

    // Save customer email
    if (isset($_POST['b2bking_request_custom_quote_email'])) {
        $customer_email = sanitize_email($_POST['b2bking_request_custom_quote_email']);
        update_post_meta($post_id, 'b2bking_request_custom_quote_email', $customer_email);
     //   update_post_meta($post_id, '_b2bking_request_custom_quote_email', $customer_email);
    }

    // Save customer phone
    if (isset($_POST['b2bking_custom_field_653730'])) {
        $customer_phone = sanitize_text_field($_POST['b2bking_custom_field_2220']);
        update_post_meta($post_id, 'b2bking_custom_field_2220', $customer_phone);
      //  update_post_meta($post_id, '_b2bking_custom_field_653730', $customer_phone);
    }
}
add_action('save_post', 'save_b2bking_custom_fields_meta');

function custom_b2bking_show_column_data($column, $post_id) {
    switch ($column) {
        case 'company_name':
            echo esc_html(get_post_meta($post_id, 'b2bking_custom_field_2224', true) ?: 'N/A');
            break;
        case 'customer_email':
            echo esc_html(get_post_meta($post_id, 'b2bking_request_custom_quote_email', true) ?: 'N/A');
            break;
        case 'customer_phone':
            echo esc_html(get_post_meta($post_id, 'b2bking_custom_field_2220', true) ?: 'N/A');
            break;
    }
}

add_action('manage_b2bking_conversation_posts_custom_column', 'custom_b2bking_show_column_data', 10, 2);

// 2️⃣ Thêm các cột mới vào bảng Conversations
function custom_b2bking_add_columns($columns) {
    $columns['company_name'] = __('Tên Công Ty', 'b2bking');
    $columns['customer_email'] = __('Email', 'b2bking');
    $columns['customer_phone'] = __('Số Điện Thoại', 'b2bking');
    return $columns;
}
add_filter('manage_edit-b2bking_conversation_columns', 'custom_b2bking_add_columns');

Attached files:

2,762
KingsPlugins replied 2 years ago on April 5, 2025 at 5:32pm

Hi there! 

 I see you're trying to display custom fields in columns for the B2BKing conversations list. I noticed a few issues in your code that are likely preventing the columns from showing up correctly:

1. There's a mismatch in the field IDs being checked:

// In save_b2bking_custom_fields_meta():
if (isset($_POST['b2bking_custom_field_653732'])) { // Wrong ID
    $company_name = sanitize_text_field($_POST['b2bking_custom_field_2224']); // Different ID
}

2. The same issue exists for the phone field:

if (isset($_POST['b2bking_custom_field_653730'])) { // Wrong ID
    $customer_phone = sanitize_text_field($_POST['b2bking_custom_field_2220']); // Different ID
}

Here's the corrected version of these sections:

// Save company name
if (isset($_POST['b2bking_custom_field_2224'])) {
    $company_name = sanitize_text_field($_POST['b2bking_custom_field_2224']);
    update_post_meta($post_id, 'b2bking_custom_field_2224', $company_name);
}

// Save customer phone
if (isset($_POST['b2bking_custom_field_2220'])) {
    $customer_phone = sanitize_text_field($_POST['b2bking_custom_field_2220']);
    update_post_meta($post_id, 'b2bking_custom_field_2220', $customer_phone);
}

Also, make sure you place the columns in a logical order by modifying the custom_b2bking_add_columns function:

function custom_b2bking_add_columns($columns) {
    $new_columns = array();
    
    // Preserve the title and other important default columns
    foreach($columns as $key => $value) {
        if($key === 'title') {
            $new_columns[$key] = $value;
            $new_columns['company_name'] = __('Tên Công Ty', 'b2bking');
            $new_columns['customer_email'] = __('Email', 'b2bking');
            $new_columns['customer_phone'] = __('Số Điện Thoại', 'b2bking');
        } else {
            $new_columns[$key] = $value;
        }
    }
    
    return $new_columns;
}

Try these corrections and let me know if you're still having issues!

Kind regards,
Stefan

Sign In