我正在构建我的第一个WP插件。基本上我应该上传一个CSV文件然后读取它值以存储在Wordpress DB的表中。在阅读了很多文档后,我按照以下步骤操作:
frequent-traveler.php
和视图部分所在的ft_admin.php
。frequent-traveler.php
中的代码:// Create database tables and some others
function frequent_traveler_activation()
{
}
register_activation_hook(__FILE__, 'frequent_traveler_activation');
// Deletes database tables and some others
function frequent_traveler_deactivation()
{
}
register_deactivation_hook(__FILE__, 'frequent_traveler_deactivation');
function frequent_traveler_admin_actions()
{
add_options_page("Frequent Traveler Configuration", "Frequent Traveler Config", 'manage_options', "ftconfig", "frequent_traveler_admin");
}
add_action('admin_menu', 'frequent_traveler_admin_actions');
function frequent_traveler_admin()
{
include('ft_admin.php');
}
if (!empty($_POST) && isset($_POST['uploadfile_btn']) && check_admin_referer('name_of_my_action', 'wpnf_ft')) {
echo "entre";
}
else {
echo "Error";
}
ft_admin.php
的代码:<?php
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
?>
<div class="wrap">
<?php echo "<h2>" . __('Frequent Traveler Configuration') . "</h2>"; ?>
<form name="frequent_traveler_form" method="post" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<input type="hidden" name="frequent_traveler_hidden" value="Y">
<?php echo "<h4>" . __('Common Settings') . "</h4>"; ?>
<p>
<?php _e("Default Conversion Value: "); ?><input type="text" name="ft_default" value="<?php echo $ft_default; ?>" size="5">
<?php _e("From Date: "); ?><input type="text" id="frequent_traveler_from_date" name="frequent_traveler_from_date" value="<?php echo $frequent_traveler_from_date ?>" class="datepicker" />
<?php _e("To Date: "); ?><input type="text" id="frequent_traveler_to_date" name="frequent_traveler_to_date" value="<?php echo $frequent_traveler_to_date; ?>" class="datepicker" />
</p>
<input type="submit" class="button-primary" name="Submit" value="<?php _e('Save Values') ?>" />
</form>
<hr/>
<?php ?>
<h2><?php echo __('Upload file to import') ?></h2>
<form name="uploadfile" id="uploadfile_form" method="POST" enctype="multipart/form-data" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']) . '/uploadfile'; ?>" accept-charset="utf-8" >
<?php wp_nonce_field('name_of_my_action', 'wpnf_ft'); ?>
<p><?php echo __('Select file to upload') ?><input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" /><p>
<input class="button-primary" type="submit" name="uploadfile_btn" id="uploadfile_btn" value="<?php echo __('Upload & Process') ?>" />
</form>
</div>
<script>
jQuery(document).ready(function() {
jQuery('.datepicker').datepicker({
dateFormat: 'dd/mm/yy'
});
});
</script>
您可能会注意到我正在尝试使用以下代码验证上传的文件:
if (!empty($_POST) && isset($_POST['uploadfile_btn']) && check_admin_referer('name_of_my_action', 'wpnf_ft')) {
echo "entre";
}
else {
echo "Error";
}
但我总是错误,为什么?我做错了什么?哪个是处理上传文件的最佳方式?
答案 0 :(得分:1)
你不应该将if (!empty($_POST)
的支票留在任何功能之外,它可以进入admin_menu
输出。
此外,form
操作应保留为空,因此会发布到同一个插件页面。
目标入队也可以用标准方式完成,并记下jQuery $
的快捷方式:
add_action( 'admin_menu', 'frequent_traveler_admin_actions' );
add_action( 'admin_enqueue_scripts', 'admin_enqueue' );
function frequent_traveler_admin_actions()
{
$my_hook = add_options_page("Frequent Traveler Configuration", "Frequent Traveler Config", 'manage_options', "ftconfig", "frequent_traveler_admin");
//var_dump($my_hook); die();
}
function admin_enqueue( $hook )
{
if( 'settings_page_ftconfig' !== $hook )
return;
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
}
function frequent_traveler_admin()
{
if (!empty($_POST) && isset($_POST['uploadfile_btn']) && check_admin_referer('name_of_my_action', 'wpnf_ft')) {
echo "<h1>entre</h1>";
}
else {
echo "<h1>Error</h1>";
}
$ft_default = '';
$frequent_traveler_from_date = '';
$frequent_traveler_to_date = '';
?>
<div class="wrap">
<?php echo "<h2>" . __('Frequent Traveler Configuration') . "</h2>"; ?>
<form name="frequent_traveler_form" method="post" action="">
<input type="hidden" name="frequent_traveler_hidden" value="Y">
<?php echo "<h4>" . __('Common Settings') . "</h4>"; ?>
<p>
<?php _e("Default Conversion Value: "); ?><input type="text" name="ft_default" value="<?php echo $ft_default; ?>" size="5">
<?php _e("From Date: "); ?><input type="text" id="frequent_traveler_from_date" name="frequent_traveler_from_date" value="<?php echo $frequent_traveler_from_date ?>" class="datepicker" />
<?php _e("To Date: "); ?><input type="text" id="frequent_traveler_to_date" name="frequent_traveler_to_date" value="<?php echo $frequent_traveler_to_date; ?>" class="datepicker" />
</p>
<input type="submit" class="button-primary" name="Submit" value="<?php _e('Save Values') ?>" />
</form>
<hr/>
<?php ?>
<h2><?php echo __('Upload file to import') ?></h2>
<form name="uploadfile" id="uploadfile_form" method="POST" enctype="multipart/form-data" action="" accept-charset="utf-8" >
<?php wp_nonce_field('name_of_my_action', 'wpnf_ft'); ?>
<p><?php echo __('Select file to upload') ?><input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" /><p>
<input class="button-primary" type="submit" name="uploadfile_btn" id="uploadfile_btn" value="<?php echo __('Upload & Process') ?>" />
</form>
</div>
<script>
jQuery(document).ready(function($) {
$('.datepicker').datepicker({
dateFormat: 'dd/mm/yy'
});
});
</script>
<?php
}