WordPress의 사용자 정의 게시물 유형 데이터를 기존 wp_posts 테이블에 삽입합니다.
<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') && 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>