Public Ticket #3846071 How to code display add field in 3 column. ( Số điện thoại - phone, email custum, Tên Công ty - Comnany ) Closed
Comments 3Hung started the conversationMarch 27, 2025 at 8:49am Hãy chỉnh sửa code giúp tôi, các trường không hiển thị lên bảng table. Please revise the code for me; the fields are not displaying on the table. Link field phone:/wp-admin/post.php?post=653730&action=edit Link field campany: /wp-admin/post.php?post=653732&action=edit Link b2bking conversation: /wp-admin/edit.php?post_type=b2bking_conversation // 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_653732', true); $customer_email = get_post_meta($post->ID, 'b2bking_request_custom_quote_email', true); $customer_phone = get_post_meta($post->ID, 'b2bking_custom_field_653730', 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_618469" 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_617057" 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_653732']); update_post_meta($post_id, 'b2bking_custom_field_653732', $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_653730']); update_post_meta($post_id, 'b2bking_custom_field_653730', $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': // Lấy dữ liệu từ post meta $company_name = get_post_meta($post_id, 'b2bking_custom_field_653732', true); if (empty($company_name)) { $company_name = get_post_meta($post_id, '_b2bking_custom_field_653732', true); } echo !empty($company_name) ? esc_html($company_name) : 'N/A'; break; case 'customer_email': $customer_email = get_post_meta($post_id, 'b2bking_request_custom_quote_email', true); if (empty($customer_email)) { $customer_email = get_post_meta($post_id, '_b2bking_request_custom_quote_email', true); } echo !empty($customer_email) ? esc_html($customer_email) : 'N/A'; break; case 'customer_phone': $customer_phone = get_post_meta($post_id, 'b2bking_custom_field_653730', true); if (empty($customer_phone)) { $customer_phone = get_post_meta($post_id, '_b2bking_custom_field_653730', true); } echo !empty($customer_phone) ? esc_html($customer_phone) : '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: image_table_show_data.png field_phone_and_field_company.png 2,568WebWizards repliedMarch 27, 2025 at 1:37pmHi Hung, Thanks for reaching out! I think there are a few issues in your code that are preventing the fields from displaying correctly in the table. Here are the main fixes needed: 1. In your `render_b2bking_custom_fields_meta_box` function, the input field names don't match the meta keys. They should be: name="b2bking_custom_field_653732" // for company name (not 618469) name="b2bking_custom_field_653730" // for phone (not 617057) 2. For proper column sorting, you should add this filter: add_filter('manage_edit-b2bking_conversation_sortable_columns', function($columns) { $columns['company_name'] = 'company_name'; $columns['customer_email'] = 'customer_email'; $columns['customer_phone'] = 'customer_phone'; return $columns; }); 3. Make sure to position your new columns correctly by modifying the columns array order in `custom_b2bking_add_columns`: function custom_b2bking_add_columns($columns) { $new_columns = array(); foreach($columns as $key => $title) { if ($key === 'title') { // Insert after title column $new_columns[$key] = $title; $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] = $title; } } return $new_columns; } Try implementing these changes and let me know if you still have any issues! Kind regards,Stefan Sign in to reply ...
Hãy chỉnh sửa code giúp tôi, các trường không hiển thị lên bảng table. Please revise the code for me; the fields are not displaying on the table. Link field phone:/wp-admin/post.php?post=653730&action=edit Link field campany: /wp-admin/post.php?post=653732&action=edit Link b2bking conversation: /wp-admin/edit.php?post_type=b2bking_conversation // 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_653732', true); $customer_email = get_post_meta($post->ID, 'b2bking_request_custom_quote_email', true); $customer_phone = get_post_meta($post->ID, 'b2bking_custom_field_653730', 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_618469" 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_617057" 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_653732']); update_post_meta($post_id, 'b2bking_custom_field_653732', $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_653730']); update_post_meta($post_id, 'b2bking_custom_field_653730', $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': // Lấy dữ liệu từ post meta $company_name = get_post_meta($post_id, 'b2bking_custom_field_653732', true); if (empty($company_name)) { $company_name = get_post_meta($post_id, '_b2bking_custom_field_653732', true); } echo !empty($company_name) ? esc_html($company_name) : 'N/A'; break; case 'customer_email': $customer_email = get_post_meta($post_id, 'b2bking_request_custom_quote_email', true); if (empty($customer_email)) { $customer_email = get_post_meta($post_id, '_b2bking_request_custom_quote_email', true); } echo !empty($customer_email) ? esc_html($customer_email) : 'N/A'; break; case 'customer_phone': $customer_phone = get_post_meta($post_id, 'b2bking_custom_field_653730', true); if (empty($customer_phone)) { $customer_phone = get_post_meta($post_id, '_b2bking_custom_field_653730', true); } echo !empty($customer_phone) ? esc_html($customer_phone) : '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: image_table_show_data.png
field_phone_and_field_company.png
Hi Hung,
Thanks for reaching out! I think there are a few issues in your code that are preventing the fields from displaying correctly in the table.
Here are the main fixes needed:
1. In your `render_b2bking_custom_fields_meta_box` function, the input field names don't match the meta keys. They should be:
2. For proper column sorting, you should add this filter:
add_filter('manage_edit-b2bking_conversation_sortable_columns', function($columns) { $columns['company_name'] = 'company_name'; $columns['customer_email'] = 'customer_email'; $columns['customer_phone'] = 'customer_phone'; return $columns; });3. Make sure to position your new columns correctly by modifying the columns array order in `custom_b2bking_add_columns`:
function custom_b2bking_add_columns($columns) { $new_columns = array(); foreach($columns as $key => $title) { if ($key === 'title') { // Insert after title column $new_columns[$key] = $title; $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] = $title; } } return $new_columns; }Try implementing these changes and let me know if you still have any issues!
Kind regards,
Stefan