我创建了一个名为album-plugin的WordPress插件,这是我的插件结构
wp-content
---plugin
--album-plugin
--album-plugin.php
--ajax_work.php
--add_album.php
--edit_album.php
--js
--css
现在这是我的album-plugin.php
的总脚本<?php
/*
Plugin Name: Album Pluggin
*/
define( 'MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) );
register_activation_hook( __FILE__, 'my_album_pluggin_activate' );
register_deactivation_hook( __FILE__, 'my_album_pluggin_deactivate' );
function my_album_pluggin_activate(){
//Activation method.
activate_album_pluggin();
}
function my_album_pluggin_deactivate(){
//Deactivation method.
global $wpdb;
$wpdb->query(" DROP TABLE ".$wpdb->prefix."album ");
$wpdb->query(" DROP TABLE ".$wpdb->prefix."image ");
}
add_action('init', 'add_ajax_work');
add_action('admin_menu', 'register_custom_album_menu_page');
function add_ajax_work()
{
include 'ajax_work.php';
}
function register_custom_album_menu_page() {
add_menu_page( 'Album Menu Display', 'Album Menu', 'read', 'album-menu', 'display_album_panel' /*, $icon_url, $position*/ );
add_submenu_page( 'album-menu', 'Add Album', 'Add Album','read', 'add-album','display_album_subpanel_add');
add_submenu_page( 'album-menu', 'Edit Album', 'Edit Album','read', 'edit-album','display_album_subpanel_edit');
}
if(isset($_POST['action']) && $_POST['action']=='add_album'){
submit_album_form();
}
if(isset($_POST['action']) && $_POST['action']=='updateRecordsListings')
{
include 'reorder.php';
}
function header_album_css_page() {
wp_register_style($handle = 'header-css', $src = plugins_url('css/album_style.css', __FILE__), $deps = array(), $ver = '1.0.0', $media = 'all');
wp_enqueue_style('header-css');
}
add_action('init', 'header_album_css_page');
function reorder_album_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_album_css_page');
function display_album_panel()
{
global $wpdb;
$banner_list = $wpdb->get_results("select * from ".$wpdb->prefix."album order by album_order desc");
?>
<div class='admin-custom'>
<?php include 'view_album.php'; ?>
</div>
<?php
}
function display_album_subpanel_add()
{ ?>
<div class='admin-custom'>
<?php include 'add_album.php'; ?>
</div>
<?php
}
function display_album_subpanel_edit()
{
global $wpdb;
$res = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."album ORDER BY album_order ASC");
?>
<div class='admin-custom'>
<?php include 'edit_album.php'; ?>
</div>
<?php
}
/*add_shortcode('DISPLAY-BANNER-SLIDER','func_display_banner');
function func_display_banner(){
global $wpdb;
$res = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."banner ORDER BY banner_order DESC");
$path = ABSPATH . 'wp-content/plugins/banner-pluggin/banner/';
$html = '<!-- banner slider-->
<div id="header">
<div class="slider-wrapper theme-default">
<div id="slider" class="nivoSlider">';
foreach($res as $row){
$html .='<img src="'.plugins_url().'/banner-pluggin/banner/'. $row->banner_img .'" alt="" title="'.$row->banner_content.'">';
}
$html .= '</div>
</div>
</div>
<!--//slider-->';
return $html;
}*/
function activate_album_pluggin(){
global $wpdb;
/*if(!file_exists(ABSPATH .'wp-content/plugins/banner-pluggin/banner/'))
mkdir(ABSPATH .'wp-content/plugins/banner-pluggin/banner/');*/
$sql_album = "CREATE TABLE IF NOT EXISTS ".$wpdb->prefix."album (
album_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
album_name VARCHAR( 255 ) NOT NULL ,
album_slug VARCHAR( 255 ) NOT NULL ,
album_content TEXT NOT NULL ,
album_order INT( 11 ) NOT NULL,
album_timestamp VARCHAR( 20 ) NOT NULL
)";
$sql_image = "CREATE TABLE IF NOT EXISTS ".$wpdb->prefix."image (
image_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
image_name VARCHAR( 255 ) NOT NULL ,
image_slug VARCHAR( 255 ) NOT NULL ,
image_img VARCHAR( 255 ) NOT NULL ,
image_content TEXT NOT NULL ,
album_id INT( 11 ) NOT NULL,
image_order INT( 11 ) NOT NULL,
image_status INT( 11 ) NOT NULL,
image_timestamp VARCHAR( 20 ) NOT NULL
)";
$wpdb->query($sql_album);
$wpdb->query($sql_image);
}
function submit_album_form(){
global $wpdb;
$albumname = $_POST['album_name'];
$albumcontent = $_POST['album_content'];
$albumtimestamp = time();
$imagename = $_POST['image_name'];
$wpdb->insert(
$wpdb->prefix."album",
array(
'album_name' => $albumname,
'album_content' => $albumcontent,
'album_timestamp' => $albumtimestamp
),
array(
'%s',
'%s',
'%s'
)
);
$album_info = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."album WHERE album_timestamp='".$albumtimestamp."'");
foreach($album_info as $row)
{
$id=$row->album_id;
}
$albumslug=$id."-".make_slug($albumname);
$update_data=array(
'album_slug'=>$albumslug
);
if(!file_exists(ABSPATH .'wp-content/plugins/album-pluggin/'.$albumslug.'/'))
mkdir(ABSPATH .'wp-content/plugins/album-pluggin/'.$albumslug.'/');
if(move_uploaded_file($_FILES["album_img"]["tmp_name"],ABSPATH . 'wp-content/plugins/album-pluggin/'.$albumslug.'/' . $_FILES["album_img"]["name"]))
echo "done";
else
echo "error";
$wpdb->update(
$wpdb->prefix."album", // Table
$update_data, // Array of key(col) => val(value to update to)
array(
'album_timestamp' => $albumtimestamp
) // Where
);
$image_link='wp-content/plugins/album-pluggin/'.$albumslug.'/'. $_FILES["album_img"]["name"];
$image_timestamp=intval(time()+10);
$wpdb->insert(
$wpdb->prefix."image",
array(
'image_name' => $imagename,
'album_id' => $id,
'image_img'=>$image_link,
'image_timestamp' => $image_timestamp
),
array(
'%s',
'%d',
'%s',
'%s'
)
);
$image_info = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."image WHERE image_timestamp='".$image_timestamp."'");
foreach($image_info as $irow)
{
$i_id=$irow->image_id;
}
$imageslug=$id."-".make_slug($imagename);
$update_image_data=array(
'image_slug'=>$imageslug
);
$wpdb->update(
$wpdb->prefix."image", // Table
$update_image_data, // Array of key(col) => val(value to update to)
array(
'image_timestamp' => $image_timestamp
) // Where
);
}
function make_slug($data)
{
$data_slug = trim($data," ");
$search = array('/','\\',':',';','!','@','#','$','%','^','*','(',')','_','+','=','|','{','}','[',']','"',"'",'<','>',',','?','~','`','&',' ','.');
$data_slug = str_replace($search, "_", $data_slug);
return $data_slug;
}
?>
现在这里是我的edit_album.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();?>/album-pluggin/js/jquery-1.7.1.min.js"></script>
<script>
function get_album_data(album_id)
{
alert(album_id);
$.post("<?php echo plugins_url();?>/album-pluggin/ajax_work.php",
{'album_id':album_id},
function(data)
{
alert(data);
}
);
}
</script>
</head>
<body>
<h1>Add Album</h1>
<form action="" method="post" enctype="multipart/form-data">
<input name="action" type="hidden" value="edit_album"/>
<div class="label_div">
<span class="label_data">Select Album</span>
</div>
<div class="text_div">
<select class="text_data" name="album_id" onchange="get_album_data(this.value);">
<option value="0">Select Album</option>
<?php
foreach($res as $r)
{?>
<option value="<?php echo $r->album_id;?>"><?php echo $r->album_name;?></option>
<?php
}?>
</select>
</div>
<div class="clear_div"></div>
<div class="label_div">
<span class="label_data">Album Name</span>
</div>
<div class="text_div">
<input class="text_data" name="album_name" type="text"/>
</div>
<div class="clear_div"></div>
<div class="label_div">
<span class="label_data">Album Image</span>
</div>
<div class="text_div">
<input name="album_img" type="file"/>
</div>
<div class="clear_div"></div>
<div class="label_div">
<span class="label_data">Image Name</span>
</div>
<div class="text_div">
<input name="image_name" type="text"/>
</div>
<div class="clear_div"></div>
<div class="label_div">
<span class="label_data">Album content</span>
</div>
<div class="text_div">
<textarea class="textarea_data" name="album_content" cols="" rows=""></textarea>
</div>
<div class="clear_div"></div>
<input name="submit" type="submit" value="Add Banner">
</form>
</body>
</html>
和最后一个ajax_work.php
<?php
global $wpdb;
$album_id=$_POST['album_id'];
$res = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."album WHERE album_id='$album_id'");
echo $res;
?>
问题是ajax工作得非常完美,问题是它会提醒消息:
<br />
<b>Fatal error</b>:
Call to a member function get_results() on a non-object in
<b>C:\xampp\htdocs\odishi\wp-content\plugins\album-pluggin\ajax_work.php</b> on line <b>4</b><br />
似乎是剧本的一部分
global $wpdb;
$album_id=$_POST['album_id'];
$res = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."album WHERE album_id='$album_id'");
遇到了一些问题。