我正在创建一个 WordPress 插件,但我遇到了一个奇怪的问题。
首先,我喜欢展示我的 WordPress 脚本。
<?php
/*
Plugin Name: Banner Plugin
*/
define( 'MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) );
register_activation_hook( __FILE__, 'my_banner_plugin_activate' );
register_deactivation_hook( __FILE__, 'my_banner_plugin_deactivate' );
function my_banner_plugin_activate(){
//Activation method.
activate();
}
function my_banner_plugin_deactivate(){
//Deactivation method.
global $wpdb;
$wpdb->query(" DROP TABLE ".$wpdb->prefix."banner ");
}
add_action('admin_menu', 'register_custom_menu_page');
function register_custom_menu_page() {
add_menu_page( 'Banner Menu Display', 'Banner Menu', 'read', 'banner-menu', 'display_banner_panel' /*, $icon_url, $position*/ );
add_submenu_page( 'banner-menu', 'Add Baner', 'Add Baner','read', 'add-banner','display_banner_subpanel_add');
}
if(isset($_POST['action']) && $_POST['action']=='add_banner'){
submit_form();
}
if(isset($_POST['action']) && $_POST['action']=='updateRecordsListings')
{
include 'reorder.php';
}
function header_css_page() {
wp_register_style($handle = 'header-css', $src = plugins_url('css/banner_style.css', __FILE__), $deps = array(), $ver = '1.0.0', $media = 'all');
wp_enqueue_style('header-css');
}
add_action('init', 'header_css_page');
function reorder_css_page() {
wp_register_style($handle = 'reorder-css', $src = plugins_url('css/reorder_style.css', __FILE__), $deps = array(), $ver = '1.0.0', $media = 'all');
wp_enqueue_style('reorder-css');
}
add_action('init', 'reorder_css_page');
function display_banner_panel()
{
global $wpdb;
$banner_list = $wpdb->get_results("select * from ".$wpdb->prefix."banner order by banner_order desc");
?>
<div class='admin-custom'>
<?php include 'view_banner.php'; ?>
</div>
<?php
}
的脚本view_banner.php
如下;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="<?php echo plugins_url();?>/banner-pluggin/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="<?php echo plugins_url();?>/banner-pluggin/js/jquery-ui-1.7.1.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(function() {
$("#contentLeft2 ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
$.post("", order, function(theResponse){
$("#contentRight2").html(theResponse);
});
}
});
});
});
</script>
</head>
<body>
<h1>Add Banner</h1>
<br/>
<br/>
<div id="contentWrap2">
<div id="contentLeft2">
<ul>
<?php
//$query = "SELECT * FROM records ORDER BY recordListingID ASC";
//$result = mysql_query($query);
//while($row = mysql_fetch_array($result, MYSQL_ASSOC))
//{
foreach($banner_list as $row)
{
?>
<li id="recordsArray_<?php echo $row->banner_id; ?>">
<div>
<img src="<?php echo plugins_url().'/banner-pluggin/banner/'. $row->banner_img;?>" height="80px" width="100px"/>
</div>
</li>
<?php } ?>
</ul>
</div>
<div id="contentRight2">
<p>Array will be displayed here.</p>
<p> </p>
</div>
</div>
</body>
</html>
如下reorder.php
;
<?php
global $wpdb;
$action = mysql_real_escape_string($_POST['action']);
$updateRecordsArray = $_POST['recordsArray'];
if ($action == "updateRecordsListings")
{
$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue)
{
$query = "UPDATE wp_banner SET banner_order = " . $listingCounter . " WHERE banner_id = " . $recordIDValue;
if(mysql_query($query))
{
}
else {
mysql_error();
}//die('Error, insert query failed');
$listingCounter = $listingCounter + 1;
}
echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>
问题是 ajax 调用也可以正常工作,唯一的问题是 ajax 响应还显示了管理菜单的一部分,
这是一个屏幕截图:
在右边的绿色框中,admin的部分不应该显示,但是为什么会显示呢?
我知道我发现了问题所在,
$.post("", order, function(theResponse)
导致 Ajax 调用被发送到同一页面,这意味着在操作之后,将像在 Ajax 中一样加载banner-pluggin.php
html 。banner-pluggin.php
但是如果我直接将 Ajax 帖子发送到reorder.php
,那么由于没有 HTML,因此不会将 HTML 作为响应加载。
但是还有另一个问题:SQL 查询没有运行,既没有显示任何错误,也没有执行 SQL