wordpress增删改查

分类: 系统运维

该插件在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() 在后台添加“设置》焦点图管理”导航链接

  1. <?php
  2. /*
  3. Plugin Name: 焦点图插件
  4. Plugin URI: http://hzm.blog.chinaunix.net
  5. Description: 该插件在wordpress-3.3.1-zh_CN版本下开发,主要用于在后台管理首页焦点图(图片轮播)
  6. Author: Henry Poter
  7. Version: 1.0
  8. Author URI: http://hzm.blog.chinaunix.net
  9. */
  10. register_activation_hook(__FILE__ , 'focusphoto_install' );
  11. register_deactivation_hook(__FILE__ , focusphoto_uninstall);
  12. function focusphoto_install() {
  13.     global $wpdb;
  14.     $table = $wpdb->prefix . 'focusphoto';
  15.     $sql = "create table $table(
  16.                  id int auto_increment primary key,
  17.                  photourl varchar(200),
  18.                  linkto varchar(200),
  19.                  title varchar(255),
  20.                  description varchar(1000)
  21.                  ) CHARSET=UTF8";
  22.     $wpdb->query($sql);
  23. }
  24. function focusphoto_uninstall(){
  25.     global $wpdb;
  26.     $table = $wpdb->prefix . 'focusphoto';
  27.     $sql = "drop table $table";
  28.     $wpdb->query($sql);
  29. }
  30. function editfocusphoto_menu()
  31. {
  32.     global $wpdb;
  33.    include 'focusphoto-admin.php';
  34. }
  35. function focusphoto_admin_actions()
  36. {
  37.     add_options_page("焦点图管理", "焦点图管理", 1,
  38. "Focus-photo", "editfocusphoto_menu");
  39. }
  40. add_action('admin_menu', 'focusphoto_admin_actions');
  41. ?>

focusphoto-admin.php 包含以下4个函数:
focusphoto_list() 焦点图列表
focusphoto_delete(
$photoid) 删除指定$photoid的记录
focusphoto_edit($photoid)  编辑
指定$photoid的记录
focusphoto_add() 添加焦点图

  1. <?php
  2. /*
  3.  * Created on Jan 31, 2012
  4.  * Author: Henry Poter
  5.  */
  6. function focusphoto_list() {
  7.     global $wpdb;
  8.     $addlink = site_url()."/wp-admin/options-general.php?page=Focus-photo&act=addfocusphoto";
  9.     $photos = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "focusphoto order by id desc limit 10");
  10.     //print_r($photos);
  11.     if (count($photos) <= 0) {
  12. ?>
  13. <div id="icon-edit" class="icon32 icon32-posts-post"><br></div>
  14. <h2>焦点图 <a href="<?php echo $addlink; ?>" class="add-new-h2">添加焦点图</a> </h2>
  15. <?php
  16.         echo "<p style='color:red;'>暂时没有焦点图,请<a href='$addlink'>点击添加</p>";
  17.     } else {
  18. ?>
  19. <div id="icon-edit" class="icon32 icon32-posts-post"><br></div>
  20. <h2>焦点图 <a href="<?php echo $addlink; ?>" class="add-new-h2">添加焦点图</a> </h2>
  21.   <table class="wp-list-table widefat fixed posts" cellspacing="0">
  22.     <thead>
  23.      <tr>
  24.         <th scope="col" class="manage-column column-cb check-column" style="">
  25.         <input type="checkbox">
  26.         </th>
  27.         <th scope="col" class="manage-column column-title" style="">
  28.         <span>标题</span><span class="sorting-indicator"></span>
  29.         </th>
  30.         <th scope="col" class=" manage-column column-title" style="">
  31.         <span>图片地址</span><span class="sorting-indicator"></span>
  32.         </th>
  33.         <th scope="col" class="manage-column column-title" style="">链接到</th>
  34.         </tr>
  35.     </thead>
  36.     <tbody id="the-list">
  37. <?php foreach ($photos as $photo) {?>
  38.             <tr id="post-1" class="post-1 post type-post status-publish format-standard hentry category-uncategorized iedit author-self" valign="top">
  39.                 <th scope="row" class="check-column"><input type="checkbox" name="post[]" value="<?php echo $photo->id;?>"></th>
  40.                 <td class="post-title page-title column-title">
  41.                  <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>
  42.                  <div class="row-actions"><span class="edit">
  43.                  <a href='?page=Focus-photo&act=editfocusphoto&photoid=<?php echo $photo->id;?>'>编辑</a> | </span>
  44.                  <span class="inline hide-if-no-js"><a href='?page=Focus-photo&act=deletefocusphoto&photoid=<?php echo $photo->id;?>'>删除</a> | </span>
  45.                  <span class="view"><a href="<?php echo $photo->photourl;?>" rel="permalink">查看焦点图</a></span>
  46.                  <span class="view"><a href="<?php echo $photo->linkto;?>" rel="permalink">查看相关链接</a></span>
  47.                  </div>
  48.                 </td>
  49.               <td class="post-title page-title column-title"><?php echo $photo->photourl;?></td>
  50.              <td class="author column-author"><?php echo $photo->linkto;?></td>
  51.             </tr>
  52. <?php }//end foreach
  53. }//end if
  54. ?>
  55.         </tbody>
  56. </table>
  57. <?php
  58.     if (isset ($_GET['photoid']) && $_GET['act'] == "editfocusphoto") {
  59.         $photoid = $_GET['photoid'];
  60.         focusphoto_edit($photoid);
  61.     }
  62.     if (isset ($_GET['photoid']) && $_GET['act'] == "deletefocusphoto") {
  63.         $photoid = $_GET['photoid'];
  64.         focusphoto_delete($photoid);
  65.     }
  66.     if (isset ($_GET['act']) && $_GET['act'] == "addfocusphoto") {
  67.         focusphoto_add();
  68.     }
  69. } //end focusphoto_list()
  70. function focusphoto_delete($photoid) {
  71.     global $wpdb;
  72.     if (!is_numeric($photoid)) {
  73.         die("<p style='color:red;'>参数photoid错误!</p>");
  74.     }
  75.     $table = $wpdb->prefix . 'focusphoto';
  76.     $result = $wpdb->query("DELETE FROM $table WHERE id = $photoid ");
  77.     if ($result == 1) {
  78.         echo "<script langue='javascript'> alert('删除成功!');</script>";
  79.         header("location: " . $_SERVER['REQUEST_URI']);
  80.     }
  81. }
  82. function focusphoto_edit($photoid) {
  83.     global $wpdb;
  84.     if (!is_numeric($photoid)) {
  85.         die("<p style='color:red;'>参数photoid错误!</p>");
  86.     }
  87.     if (isset ($_POST['editphoto'])) {
  88.         $newphoto = array (
  89.             "photourl" => $_POST['photourl'],
  90.             "linkto" => $_POST['linkto'],
  91.             "title" => $_POST['title']
  92.         );
  93.         print_r($newphoto);
  94.         $result = $wpdb->update($wpdb->prefix . "focusphoto", $newphoto, array (
  95.             'id' => $photoid
  96.         ), $format = null, $where_format = null);
  97.         //if($result == 1){
  98.         echo "<script langue='javascript'> alert('编辑成功!');</script>";
  99.         header("location: " . site_url().'/wp-admin/options-general.php?page=Focus-photo');
  100.         //}
  101.     }
  102.     $photo = $wpdb->get_results("SELECT * FROM " .
  103.     $wpdb->prefix . "focusphoto" . " WHERE id=$photoid");
  104.     // print_r($photo);
  105. ?>
  106. <br/>
  107. <form action="" method="post">
  108.      <table class="widefat" cellspacing="0" >
  109.      <thead>
  110.      <tr>
  111.         <th scope="col" class="manage-column column-title" colspan="4">编辑焦点图
  112.         </th>
  113.      </tr>
  114.     </thead>
  115.       <tbody>
  116.         <tr><td></td><td></td></tr>
  117.         <tr><td>图片地址</td><td><input size="80" tabindex="1" autocomplete="off" type='text' value='<?php echo $photo[0]->photourl ;?>' name='photourl' > </td></tr>
  118.         <tr><td>链接到</td><td><input size="80" tabindex="2" type='text' value='<?php echo $photo[0]->linkto ;?>' name='linkto' ></td></tr>
  119.         <tr><td>标题</td><td><input size="80" tabindex="3" type='text' value='<?php echo $photo[0]->title ;?>' name='title' ></td></tr>
  120.         <tr><td></td><td><input tabindex="4" type='submit' name='editphoto' value='保存' style='80px;'></td></tr>
  121.        </tbody>
  122.      </table>
  123.    </form>
  124. <?php
  125. } //end focusphoto_edit()
  126. function focusphoto_add() {
  127.     global $wpdb;
  128.     if (isset ($_POST['addphoto'])) {
  129.         $photo = array (
  130.             "photourl" => $_POST['photourl'],
  131.             "linkto" => $_POST['linkto'],
  132.             "title" => $_POST['title']
  133.         );
  134.         $wpdb->insert($wpdb->prefix . "focusphoto", $photo);
  135.         header("location: " . $_SERVER['REQUEST_URI']);
  136.     }
  137. ?>
  138. <br/>
  139.    <form action="" method="post">
  140.      <table class="widefat" cellspacing="0">
  141.      <thead>
  142.      <tr>
  143.         <th scope="col" class="manage-column column-title" colspan="4">添加焦点图
  144.         </th>
  145.      </tr>
  146.     </thead>
  147.       <tbody>
  148.        <tr><td></td><td></td></tr>
  149.         <tr><td>图片地址</td><td><input size="80" tabindex="1" type='text' value='' name='photourl' > </td></tr>
  150.         <tr><td>链接到</td><td><input size="80" tabindex="2" type='text' value='' name='linkto' ></td></tr>
  151.         <tr><td>标题</td><td><input size="80" tabindex="3" type='text' value='' name='title' ></td></tr>
  152.         <tr><td></td><td><input tabindex="4" type='submit' name='addphoto' value='添加' style='80px;'></td></tr>
  153.       </tbody>
  154.      </table>
  155.    </form>
  156. <?php
  157. } //end focusphoto_add()
  158. focusphoto_list();
  159. ?>
原文地址:https://www.cnblogs.com/qqyuhaitao/p/3291820.html