Untitled

 avatar
unknown
plain_text
2 months ago
3.7 kB
5
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');
Editor is loading...
Leave a Comment