如果要存储单个值,请使用以下代码。。
update_option( \'plugin_timestamp_data\', $timestamp);
$timestamp = get_option( \'plugin_timestamp_data\' );
https://codex.wordpress.org/Function_Reference/update_option
如果要保存多个条目,可以使用自定义表存储时间戳数据。。
//Define table name to use everywhere
define( \'TABLE_NAME\', \'plugin_timestamp_data\' );
下面是初始化插件的代码
global $jal_db_version;
$jal_db_version = \'1.0\';
function jal_install() {
global $wpdb;
global $jal_db_version;
$table_name = TABLE_NAME;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
time DATETIME NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );
add_option( \'jal_db_version\', $jal_db_version );
}
//Calling the activation hook
register_activation_hook( __FILE__, \'jal_install\' );
要删除插件卸载时的数据,请使用以下代码。。
function jal_uninstall() {
global $wpdb;
$table_name = TABLE_NAME;
$dropsql = "DROP TABLE IF EXISTS {$table_name};";
$wpdb->query( $dropsql );
}
//Calling the deactivation hook
register_deactivation_hook( __FILE__, \'jal_uninstall\' );
要添加时间戳值,只需在插件文件中声明下面的函数,并在任何需要的地方调用它。。
function jal_insert_record ($timestamp) {
global $wpdb;
$table_name = TABLE_NAME;
$wpdb->insert(
$table_name,
array(
\'time\' => $timestamp
)
);
}
https://codex.wordpress.org/Creating_Tables_with_Plugins