将WordPress的自定义文章类型数据插入到现有的wp_posts表中。
P粉811329034
P粉811329034 2023-07-28 16:24:13
0
1
365
<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插件的惯例是在删除插件时删除额外的数据,而不是在停用插件时删除。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板