Untitled

 avatar
unknown
plain_text
25 days ago
5.5 kB
3
Indexable
// 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_618469', true);
    $customer_email = get_post_meta($post->ID, 'b2bking_request_custom_quote_email', true);
    $customer_phone = get_post_meta($post->ID, 'b2bking_custom_field_617057', 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_618469'])) {
        $company_name = sanitize_text_field($_POST['b2bking_custom_field_618469']);
        update_post_meta($post_id, 'b2bking_custom_field_618469', $company_name);
        update_post_meta($post_id, '_b2bking_custom_field_618469', $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_617057'])) {
        $customer_phone = sanitize_text_field($_POST['b2bking_custom_field_617057']);
        update_post_meta($post_id, 'b2bking_custom_field_617057', $customer_phone);
        update_post_meta($post_id, '_b2bking_custom_field_617057', $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_618469', true);
            if (empty($company_name)) {
                $company_name = get_post_meta($post_id, '_b2bking_custom_field_618469', 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_617057', true);
            if (empty($customer_phone)) {
                $customer_phone = get_post_meta($post_id, '_b2bking_custom_field_617057', 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');

Editor is loading...
Leave a Comment