我在这里编写了我的第一个wordpress插件代码:
<?php
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'PLUGIN_NAME_VERSION', '1.0.0' );
/**
* The code that runs during plugin activation.
* This action is documented in includes/class-post-time-changer-activator.php
*/
function activate_post_time_changer() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-post-time-changer-activator.php';
Post_Time_Changer_Activator::activate();
}
/**
* The code that runs during plugin deactivation.
* This action is documented in includes/class-post-time-changer-deactivator.php
*/
function deactivate_post_time_changer() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-post-time-changer-deactivator.php';
Post_Time_Changer_Deactivator::deactivate();
}
register_activation_hook( __FILE__, 'activate_post_time_changer' );
register_deactivation_hook( __FILE__, 'deactivate_post_time_changer' );
/**
* The core plugin class that is used to define internationalization,
* admin-specific hooks, and public-facing site hooks.
*/
require plugin_dir_path( __FILE__ ) . 'includes/class-post-time-changer.php';
/**
* Begins execution of the plugin.
*
* Since everything within the plugin is registered via hooks,
* then kicking off the plugin from this point in the file does
* not affect the page life cycle.
*
* @since 1.0.0
*/
function run_post_time_changer() {
$plugin = new Post_Time_Changer();
$plugin->run();
}
run_post_time_changer();
add_action('admin_menu', 'PTC_setup_menu');
function PTC_setup_menu(){
add_menu_page( 'Post Time Changer', 'PostTimeChanger', 'manage_options', 'post-time-changer', 'ptc_option_page' );
}
global $ptc_db_version;
$ptc_db_version = '1.0';
//install database table
function ptc_install() {
global $wpdb;
global $ptc_db_version;
$table_name = $wpdb->prefix . 'concilium';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
hours tinytext NOT NULL,
minutes tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'ptc_db_version', $ptc_db_version );
}
//initial database data
function ptc_install_data() {
global $wpdb;
$hours = '6';
$minutes = '12';
$table_name = $wpdb->prefix . 'concilium';
$wpdb->insert(
$table_name,
array(
'hours' => $hours,
'minutes' => $minutes,
)
);
}
function ptc_table_check(){
global $wpdb;
$table_name = $wpdb->prefix . 'concilium';
$checktable = mysql_query('select 1 from' . $table_name );
if($checktable !== FALSE)
{
ptc_install();
echo "database installed!";
}else{
ptc_update_data();
echo "data upadated!";
}
}
//update data
function ptc_update_data(){
global $wpdb;
$hours = $_POST['hours'];
$minutes = $_POST['minutes'];
$table = 'wp_concilium';
$rows = $wpdb->update(
$table,
array(
'hours' => $hours, // string
'minutes' => $minutes // integer (number)
),
array( 'ID' => 1 ),
array(
'%s', // value1
'%d' // value2
),
array( '%d' )
);
}
function ptc_option_page(){
?>
<div class="wrap">
<h1>Post Time Changer</h1>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_concilium");
foreach ( $result as $print) {
?>
<?php
}
?>
<form action="<?php ptc_update_data(); ?>" method="post">
<?php settings_fields('plugin_options'); ?>
<?php do_settings_sections('plugin'); ?>
<!-- amount of time the post should be anticipated -->
set the amount of Hours you want your post to be anticipated <input name="hours" type="number" value="" /><br/>
set the amount of minutes you want your post to be anticipated <input name="minutes" type="number" value="" /><br/>
<tr>
<td><h2>currently set to</h2></td>
<td><?php echo $print->hours;?> hours</td>
<td><?php echo $print->minutes;?> minutes</td>
</tr><b/>
<!--save setting on the bottom -->
<input name="Submit" type="submit" value="update" />
</form></div>
<?php
add_action('admin_init', 'plugin_admin_init');
function plugin_admin_init(){
register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'plugin');
add_settings_field('plugin_text_string', 'Plugin Text Input', 'plugin_setting_string', 'plugin', 'plugin_main');
}
}
插件会更新数据库,其中包含我希望发布帖子的小时和分钟数,以便人们可以认为我已经在实际时间之前几个小时发布了。 代码正常工作,因为在代码部分:
<tr>
<td><h2>currently set to</h2></td>
<td><?php echo $print->hours;?> hours</td>
<td><?php echo $print->minutes;?> minutes</td>
</tr><b/>
它没有显示它在数据库中保存的正确时间,但是当我重新加载页面时它显示了保存的时间,这是我重新加载数据库字段时的另一个问题返回0
我该如何解决这个问题?