將WordPress的自訂文章類型資料插入到現有的wp_posts表中。
P粉811329034
P粉811329034 2023-07-28 16:24:13
0
1
373
<p>我已经创建了一个插件,可以在WordPress数据库中的现有wp_posts表上运行。为此,我创建了插件的代码,当激活插件时,它会向现有数据库添加3个字段,并在停用插件时从现有数据库中删除这3个字段。</p><p>这是我布局的屏幕截图。<br />我希望在在字段中添加值后,当我点击更新时,地址、纬度和经度字段将把数据显示到数据库中。</p><p>以下是代码:</p><p><br /></p> <pre class="brush:php;toolbar:false;"><?php /** * Plugin Name: Hello World * Plugin URI: https://example.com/my-plugin * Description: This is a description of my hello world sample plugin. * Version: 2.3.1 * Author: Selin * Author URI: https://example.com * License: GPL v2 or later * License URI: https://www.gnu.org/licenses/gpl-2.0.html */ include_once WP_PLUGIN_DIR . '/form-data/connection.php'; include_once(__DIR__. 'AdminStorecreatefile.php'); include_once(__DIR__ . 'AdminStoreview.php'); //global $post; function db_activate() { global $wpdb; $table = $wpdb->prefix . 'posts'; $charset_collate = $wpdb->get_charset_collate(); $sql = "ALTER TABLE $table ADD `address` VARCHAR(255) NOT NULL, ADD `longitude` VARCHAR(20) NOT NULL, ADD `latitude` VARCHAR(20) NOT NULL ;"; $wpdb->query($sql); $wpdb->get_results("DESCRIBE $table"); } register_activation_hook(__FILE__, 'db_activate'); function db_deactivate() { global $wpdb; $table = $wpdb->prefix . 'posts'; $sql = "ALTER TABLE $table DROP COLUMN `address`, DROP COLUMN `longitude`, DROP COLUMN `latitude`"; $wpdb->query($sql); } register_deactivation_hook(__FILE__, 'db_deactivate'); // custom-post-type function custom_store_metaboxes() { add_meta_box( 'store_location', 'Store Location', 'display_store', 'store', 'normal', 'high' ); } function display_store($post) { // Get existing address, latitude, and longitude values (if any) $address = get_post_meta($post->ID, 'address', true); $latitude = get_post_meta($post->ID, 'latitude', true); $longitude = get_post_meta($post->ID, 'longitude', true); ?> <label for="address">Address:</label><br> <input type="text" id="address" name="address" value="<?php echo esc_attr($address); ?>"/><br><br> <label for="latitude">Latitude:</label><br> <input type="text" name="latitude" id="latitude" value="<?php echo esc_attr($latitude); ?>" /><br><br> <label for="longitude">Longitude:</label><br> <input type="text" name="longitude" id="longitude" value="<?php echo esc_attr($longitude); ?>" /><br><br> <?php } function save_store_location_fields($post_id) { if (defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE) { return; } if (!current_user_can('edit_post', $post_id)) { return; } // Insert or update the address if (isset($_POST['address'])) { update_post_meta($post_id, 'address', sanitize_text_field($_POST['address'])); } // Insert or update the latitude if (isset($_POST['latitude'])) { update_post_meta($post_id, 'latitude', sanitize_text_field($_POST['latitude'])); } // Insert or update the longitude if (isset($_POST['longitude'])) { update_post_meta($post_id, 'longitude', sanitize_text_field($_POST['longitude'])); } } add_action('save_post_store', 'save_store_location_fields'); //wp_insert_post($address,$latitude,$longitude); add_action('add_meta_boxes_store', 'custom_store_metaboxes'); //add_action('save_post_store', 'save_store_location_fields'); function register_custom_store_post_type() { $labels = array( 'name' => _x( 'Store Locator', 'Post type general name', 'textdomain' ), 'singular_name' => _x( 'Store', 'Post type singular name', 'textdomain' ), 'menu_name' => _x( 'Stores', 'Admin Menu text', 'textdomain' ), 'name_admin_bar' => _x( 'Store', 'Add New on Toolbar', 'textdomain' ), 'add_new' => __( 'Add New', 'textdomain' ), 'add_new_item' => __( 'Add New Store', 'textdomain' ), 'new_item' => __( 'New Store', 'textdomain' ), 'edit_item' => __( 'Edit Store', 'textdomain' ), 'view_item' => __( 'View Store', 'textdomain' ), 'all_items' => __( 'All Stores', 'textdomain' ), 'search_items' => __( 'Search Stores', 'textdomain' ), 'parent_item_colon' => __( 'Parent Stores:', 'textdomain' ), 'not_found' => __( 'No stores found.', 'textdomain' ), 'not_found_in_trash' => __( 'No stores found in Trash.', 'textdomain' ), 'featured_image' => _x( 'Store Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ), 'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ), 'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ), 'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ), 'archives' => _x( 'Store archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ), 'insert_into_item' => _x( 'Insert into store', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ), 'uploaded_to_this_item' => _x( 'Uploaded to this store', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ), 'filter_items_list' => _x( 'Filter stores list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ), 'items_list_navigation' => _x( 'Stores list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ), 'items_list' => _x( 'Stores list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ), ); $args = array( 'labels' => $labels, 'description' => 'Store custom post type.', 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'store' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 10, 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ), 'menu_icon' => 'dashicons-location', 'show_in_rest' => true, 'register_meta_box_cb' => 'custom_store_metaboxes' ); register_post_type('store', $args); } add_action('init', 'register_custom_store_post_type'); ?></pre> <p>我想要在地址、纬度和经度字段中插入记录,请帮我编写这部分代码。</p>
P粉811329034
P粉811329034

全部回覆(1)
P粉393030917

我腦海中的警鐘正大聲鳴響。請不要在核心表中新增列。為什麼呢?

核心版本更新(例如即將發布的6.2.2到6.3更新)會將核心表的結構還原為標準。核心更新要么會失敗,要么會幫助您刪除額外的列。

核心具有處理類似您的元資料的快取子系統,儲存在wp_postmeta表中。您可以利用它並獲得性能上的好處。

WordPress生態系統中充斥著使用這些表格的程式碼(核心、外掛程式、主題)。例如,插件儲存庫中有成千上萬個插件。除了它是否工作,沒有關於該代碼正確性的標準。您將遇到那些假設它知道posts表中有哪些列,並在其他列存在時執行奇怪操作的程式碼。我在這裡盡量禮貌。事實是,WordPress生態系統中包含了許多基本可用的糟糕程式碼。透過像是向核心表中新增列這樣奇怪的操作,您將引發這些糟糕程式碼的問題。

有人在評論中指出,wp_postmeta表是存放您的資料的地方。這是正確的。您已經在使用update_post_meta()進行操作,基本上是正確的。

順便說一下,WordPress外掛的慣例是在刪除外掛程式時刪除額外的數據,而不是在停用外掛程式時刪除。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板