diff --git a/Post_Meta/meta_boxes/add_post_meta_to_separate_table/index.php b/Post_Meta/meta_boxes/add_post_meta_to_separate_table/index.php new file mode 100644 index 0000000..5d64a7e --- /dev/null +++ b/Post_Meta/meta_boxes/add_post_meta_to_separate_table/index.php @@ -0,0 +1,96 @@ +prefix . 'custom_meta_table'; // Table name + + // Set the charset and collation + $charset_collate = $wpdb->get_charset_collate(); + + // SQL to create the table + $sql = "CREATE TABLE $table_name ( + id mediumint(9) NOT NULL AUTO_INCREMENT, + post_id bigint(20) NOT NULL, + custom_field_value varchar(255) DEFAULT '' NOT NULL, + PRIMARY KEY (id), + KEY post_id (post_id) + ) $charset_collate;"; + + // Include the upgrade file which contains the dbDelta() function + require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); + + // Run the SQL to create the table + dbDelta($sql); +} +register_activation_hook(__FILE__, 'create_custom_meta_table'); + + +function add_custom_meta_box() { + add_meta_box( + 'custom_meta_box', // ID for the meta box + 'Custom Meta Box', // Title of the meta box + 'custom_meta_box_callback', // Callback function to display the content + 'post', // Post type (you can add other types like 'page' or custom post types) + 'normal', // Context (where the meta box appears: normal, side, advanced) + 'high' // Priority (high, core, default, low) + ); +} +add_action('add_meta_boxes', 'add_custom_meta_box'); + + + +function custom_meta_box_callback($post) { + // Add nonce field for security + wp_nonce_field('custom_meta_box_nonce', 'meta_box_nonce'); + + // Get the current value of the custom field from the database (if any) + $custom_value = get_post_meta($post->ID, '_custom_field', true); + + // Display the form field + echo ''; + echo ''; +} + + + +function save_custom_meta_data($post_id) { + // Check if it's an autosave or if the nonce is not valid + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; + if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'custom_meta_box_nonce')) return $post_id; + + // Check if the user has permission to edit the post + if ('post' != $_POST['post_type'] || !current_user_can('edit_post', $post_id)) return $post_id; + + // Get the posted value + $custom_value = isset($_POST['custom_field']) ? sanitize_text_field($_POST['custom_field']) : ''; + + // Insert or update the custom value in the custom table + global $wpdb; + $table_name = $wpdb->prefix . 'custom_meta_table'; + + // Check if this post already has an entry in the custom table + $existing_entry = $wpdb->get_var($wpdb->prepare("SELECT id FROM $table_name WHERE post_id = %d", $post_id)); + + if ($existing_entry) { + // Update the existing row + $wpdb->update( + $table_name, + array('custom_field_value' => $custom_value), + array('post_id' => $post_id), + array('%s'), + array('%d') + ); + } else { + // Insert a new row + $wpdb->insert( + $table_name, + array('post_id' => $post_id, 'custom_field_value' => $custom_value), + array('%d', '%s') + ); + } + + return $post_id; +} +add_action('save_post', 'save_custom_meta_data'); + +