wordpress 焦点图插件-增删改查操作 2012-02-01 15:39:14
分类: 系统运维
该插件在wordpress-3.3.1-zh_CN版本下开发,主要用于在后台管理首页焦点图(图片轮播)。
存放焦点图信息的表 focusphoto(id,photourl,linkto,title,description)
该插件包括2个文件 focusphoto.php和focusphoto-admin.php
效果如图:
具体代码如下:
focusphoto.php 包含以下函数:
focusphoto_install() 创建表focusphoto(id,photourl,linkto,title,description)
focusphoto_uninstall() 删除表
editfocusphoto_menu() focusphoto_admin_actions() 在后台添加“设置》焦点图管理”导航链接
focusphoto-admin.php 包含以下4个函数:
focusphoto_list() 焦点图列表
focusphoto_delete($photoid) 删除指定$photoid的记录
focusphoto_edit($photoid) 编辑指定$photoid的记录
focusphoto_add() 添加焦点图
存放焦点图信息的表 focusphoto(id,photourl,linkto,title,description)
该插件包括2个文件 focusphoto.php和focusphoto-admin.php
效果如图:
具体代码如下:
focusphoto.php 包含以下函数:
focusphoto_install() 创建表focusphoto(id,photourl,linkto,title,description)
focusphoto_uninstall() 删除表
editfocusphoto_menu() focusphoto_admin_actions() 在后台添加“设置》焦点图管理”导航链接
- <?php
- /*
- Plugin Name: 焦点图插件
- Plugin URI: http://hzm.blog.chinaunix.net
- Description: 该插件在wordpress-3.3.1-zh_CN版本下开发,主要用于在后台管理首页焦点图(图片轮播)
- Author: Henry Poter
- Version: 1.0
- Author URI: http://hzm.blog.chinaunix.net
- */
- register_activation_hook(__FILE__ , 'focusphoto_install' );
- register_deactivation_hook(__FILE__ , focusphoto_uninstall);
- function focusphoto_install() {
- global $wpdb;
- $table = $wpdb->prefix . 'focusphoto';
- $sql = "create table $table(
- id int auto_increment primary key,
- photourl varchar(200),
- linkto varchar(200),
- title varchar(255),
- description varchar(1000)
- ) CHARSET=UTF8";
- $wpdb->query($sql);
- }
- function focusphoto_uninstall(){
- global $wpdb;
- $table = $wpdb->prefix . 'focusphoto';
- $sql = "drop table $table";
- $wpdb->query($sql);
- }
- function editfocusphoto_menu()
- {
- global $wpdb;
- include 'focusphoto-admin.php';
- }
- function focusphoto_admin_actions()
- {
- add_options_page("焦点图管理", "焦点图管理", 1,
- "Focus-photo", "editfocusphoto_menu");
- }
- add_action('admin_menu', 'focusphoto_admin_actions');
- ?>
focusphoto-admin.php 包含以下4个函数:
focusphoto_list() 焦点图列表
focusphoto_delete($photoid) 删除指定$photoid的记录
focusphoto_edit($photoid) 编辑指定$photoid的记录
focusphoto_add() 添加焦点图
- <?php
- /*
- * Created on Jan 31, 2012
- * Author: Henry Poter
- */
- function focusphoto_list() {
- global $wpdb;
- $addlink = site_url()."/wp-admin/options-general.php?page=Focus-photo&act=addfocusphoto";
- $photos = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "focusphoto order by id desc limit 10");
- //print_r($photos);
- if (count($photos) <= 0) {
- ?>
- <div id="icon-edit" class="icon32 icon32-posts-post"><br></div>
- <h2>焦点图 <a href="<?php echo $addlink; ?>" class="add-new-h2">添加焦点图</a> </h2>
- <?php
- echo "<p style='color:red;'>暂时没有焦点图,请<a href='$addlink'>点击添加</p>";
- } else {
- ?>
- <div id="icon-edit" class="icon32 icon32-posts-post"><br></div>
- <h2>焦点图 <a href="<?php echo $addlink; ?>" class="add-new-h2">添加焦点图</a> </h2>
- <table class="wp-list-table widefat fixed posts" cellspacing="0">
- <thead>
- <tr>
- <th scope="col" class="manage-column column-cb check-column" style="">
- <input type="checkbox">
- </th>
- <th scope="col" class="manage-column column-title" style="">
- <span>标题</span><span class="sorting-indicator"></span>
- </th>
- <th scope="col" class=" manage-column column-title" style="">
- <span>图片地址</span><span class="sorting-indicator"></span>
- </th>
- <th scope="col" class="manage-column column-title" style="">链接到</th>
- </tr>
- </thead>
- <tbody id="the-list">
- <?php foreach ($photos as $photo) {?>
- <tr id="post-1" class="post-1 post type-post status-publish format-standard hentry category-uncategorized iedit author-self" valign="top">
- <th scope="row" class="check-column"><input type="checkbox" name="post[]" value="<?php echo $photo->id;?>"></th>
- <td class="post-title page-title column-title">
- <strong><a class="row-title" href="?page=Focus-photo&act=editfocusphoto&photoid=<?php echo $photo->id;?>" title="<?php echo $photo->title;?>"><?php echo $photo->title;?></a></strong>
- <div class="row-actions"><span class="edit">
- <a href='?page=Focus-photo&act=editfocusphoto&photoid=<?php echo $photo->id;?>'>编辑</a> | </span>
- <span class="inline hide-if-no-js"><a href='?page=Focus-photo&act=deletefocusphoto&photoid=<?php echo $photo->id;?>'>删除</a> | </span>
- <span class="view"><a href="<?php echo $photo->photourl;?>" rel="permalink">查看焦点图</a></span>
- <span class="view"><a href="<?php echo $photo->linkto;?>" rel="permalink">查看相关链接</a></span>
- </div>
- </td>
- <td class="post-title page-title column-title"><?php echo $photo->photourl;?></td>
- <td class="author column-author"><?php echo $photo->linkto;?></td>
- </tr>
- <?php }//end foreach
- }//end if
- ?>
- </tbody>
- </table>
- <?php
- if (isset ($_GET['photoid']) && $_GET['act'] == "editfocusphoto") {
- $photoid = $_GET['photoid'];
- focusphoto_edit($photoid);
- }
- if (isset ($_GET['photoid']) && $_GET['act'] == "deletefocusphoto") {
- $photoid = $_GET['photoid'];
- focusphoto_delete($photoid);
- }
- if (isset ($_GET['act']) && $_GET['act'] == "addfocusphoto") {
- focusphoto_add();
- }
- } //end focusphoto_list()
- function focusphoto_delete($photoid) {
- global $wpdb;
- if (!is_numeric($photoid)) {
- die("<p style='color:red;'>参数photoid错误!</p>");
- }
- $table = $wpdb->prefix . 'focusphoto';
- $result = $wpdb->query("DELETE FROM $table WHERE id = $photoid ");
- if ($result == 1) {
- echo "<script langue='javascript'> alert('删除成功!');</script>";
- header("location: " . $_SERVER['REQUEST_URI']);
- }
- }
- function focusphoto_edit($photoid) {
- global $wpdb;
- if (!is_numeric($photoid)) {
- die("<p style='color:red;'>参数photoid错误!</p>");
- }
- if (isset ($_POST['editphoto'])) {
- $newphoto = array (
- "photourl" => $_POST['photourl'],
- "linkto" => $_POST['linkto'],
- "title" => $_POST['title']
- );
- print_r($newphoto);
- $result = $wpdb->update($wpdb->prefix . "focusphoto", $newphoto, array (
- 'id' => $photoid
- ), $format = null, $where_format = null);
- //if($result == 1){
- echo "<script langue='javascript'> alert('编辑成功!');</script>";
- header("location: " . site_url().'/wp-admin/options-general.php?page=Focus-photo');
- //}
- }
- $photo = $wpdb->get_results("SELECT * FROM " .
- $wpdb->prefix . "focusphoto" . " WHERE id=$photoid");
- // print_r($photo);
- ?>
- <br/>
- <form action="" method="post">
- <table class="widefat" cellspacing="0" >
- <thead>
- <tr>
- <th scope="col" class="manage-column column-title" colspan="4">编辑焦点图
- </th>
- </tr>
- </thead>
- <tbody>
- <tr><td></td><td></td></tr>
- <tr><td>图片地址</td><td><input size="80" tabindex="1" autocomplete="off" type='text' value='<?php echo $photo[0]->photourl ;?>' name='photourl' > </td></tr>
- <tr><td>链接到</td><td><input size="80" tabindex="2" type='text' value='<?php echo $photo[0]->linkto ;?>' name='linkto' ></td></tr>
- <tr><td>标题</td><td><input size="80" tabindex="3" type='text' value='<?php echo $photo[0]->title ;?>' name='title' ></td></tr>
- <tr><td></td><td><input tabindex="4" type='submit' name='editphoto' value='保存' style='width:80px;'></td></tr>
- </tbody>
- </table>
- </form>
- <?php
- } //end focusphoto_edit()
- function focusphoto_add() {
- global $wpdb;
- if (isset ($_POST['addphoto'])) {
- $photo = array (
- "photourl" => $_POST['photourl'],
- "linkto" => $_POST['linkto'],
- "title" => $_POST['title']
- );
- $wpdb->insert($wpdb->prefix . "focusphoto", $photo);
- header("location: " . $_SERVER['REQUEST_URI']);
- }
- ?>
- <br/>
- <form action="" method="post">
- <table class="widefat" cellspacing="0">
- <thead>
- <tr>
- <th scope="col" class="manage-column column-title" colspan="4">添加焦点图
- </th>
- </tr>
- </thead>
- <tbody>
- <tr><td></td><td></td></tr>
- <tr><td>图片地址</td><td><input size="80" tabindex="1" type='text' value='' name='photourl' > </td></tr>
- <tr><td>链接到</td><td><input size="80" tabindex="2" type='text' value='' name='linkto' ></td></tr>
- <tr><td>标题</td><td><input size="80" tabindex="3" type='text' value='' name='title' ></td></tr>
- <tr><td></td><td><input tabindex="4" type='submit' name='addphoto' value='添加' style='width:80px;'></td></tr>
- </tbody>
- </table>
- </form>
- <?php
- } //end focusphoto_add()
- focusphoto_list();
- ?>