將WordPress的自訂文章類型資料插入到現(xiàn)有的wp_posts表中。
P粉811329034
2023-07-28 16:24:13
<p>我已經(jīng)創(chuàng)建了一個插件,可以在WordPress數(shù)據(jù)庫中的現(xiàn)有wp_posts表上運行。為此,我創(chuàng)建了插件的代碼,當(dāng)激活插件時,它會向現(xiàn)有數(shù)據(jù)庫添加3個字段,并在停用插件時從現(xiàn)有數(shù)據(jù)庫中刪除這3個字段。</p><p>這是我布局的屏幕截圖。<br />我希望在在字段中添加值后,當(dāng)我點擊更新時,地址、緯度和經(jīng)度字段將把數(shù)據(jù)顯示到數(shù)據(jù)庫中。</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') && 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>我想要在地址、緯度和經(jīng)度字段中插入記錄,請幫我編寫這部分代碼。</p>
我腦海中的警鐘正大聲鳴響。請不要在核心表中新增列。為什麼呢?
核心版本更新(例如即將發(fā)布的6.2.2到6.3更新)會將核心表的結(jié)構(gòu)還原為標準。核心更新要么會失敗,要么會幫助您刪除額外的列。
核心具有處理類似您的元資料的快取子系統(tǒng),儲存在wp_postmeta表中。您可以利用它並獲得性能上的好處。
WordPress生態(tài)系統(tǒng)中充斥著使用這些表格的程式碼(核心、外掛程式、主題)。例如,插件儲存庫中有成千上萬個插件。除了它是否工作,沒有關(guān)於該代碼正確性的標準。您將遇到那些假設(shè)它知道posts表中有哪些列,並在其他列存在時執(zhí)行奇怪操作的程式碼。我在這裡盡量禮貌。事實是,WordPress生態(tài)系統(tǒng)中包含了許多基本可用的糟糕程式碼。透過像是向核心表中新增列這樣奇怪的操作,您將引發(fā)這些糟糕程式碼的問題。
有人在評論中指出,wp_postmeta表是存放您的資料的地方。這是正確的。您已經(jīng)在使用update_post_meta()進行操作,基本上是正確的。
順便說一下,WordPress外掛的慣例是在刪除外掛程式時刪除額外的數(shù)據(jù),而不是在停用外掛程式時刪除。
點贊 +0
P粉811329034