wordpress 表单插件

需求: 做一个页面,收集用户的申请意向,需要自定义表单

首先需要创建数据表

function install_coopertion() {
	global $wpdb;
  $table_name = $wpdb->prefix . "thetable";

  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
  	$sql = "CREATE TABLE " . $table_name . " (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
		  time bigint(11) DEFAULT '0' NOT NULL,
		  name text NOT NULL,
		  phone text NOT NULL,
		  status int,
		  UNIQUE KEY id (id)
		);";

	  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	  dbDelta($sql);
  }
}

删除数据表

function drop_coopertion() {
	// global $wpdb;
  // $table_name = $wpdb->prefix . "thetable";

  // $wpdb->query("DROP TABLE IF EXISTS " . $table_name);
}

注册

// 插件激活时,运行回调方法创建数据表, 在WP中添加表
register_activation_hook(__FILE__, 'install_coopertion');
// 插件停用时,运行回调方法删除数据表,删除表

register_deactivation_hook(__FILE__, 'drop_coopertion');


接下来创建菜单

function cooperation_edit_menu()
{
   global $wpdb;
   include 'cooperact.php';
}

function cooperation_admin_actions()
{
  add_menu_page(
   	"合作意向申请", 页面的 title, 和显示在 <title> 标签里的一样
   	"申请列表", //在控制面板中显示的名称
   	'administrator', //要浏览菜单所要的最低权限
   	__FILE__, // 要引用该菜单的别名; 必须是唯一的
   	"cooperation_edit_menu", // 要显示菜单对应的页面内容所调用的函数
   	admin_url( '/images/media-button.png', __FILE ) //菜单的 icon 图片的 URL
   );
}

注册

add_action('admin_menu', 'cooperation_admin_actions');


完整代码

Plugin Name: 合作意向申请
Description: 合作意向申请表单.
Author: Donald
Version: 1.0
Author Donald
*/

function install_coopertion() {
	global $wpdb;
  $table_name = $wpdb->prefix . "thetable";

  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
  	$sql = "CREATE TABLE " . $table_name . " (
		  id mediumint(9) NOT NULL AUTO_INCREMENT,
		  time bigint(11) DEFAULT '0' NOT NULL,
		  name text NOT NULL,
		  phone text NOT NULL,
		  status int,
		  UNIQUE KEY id (id)
		);";

	  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	  dbDelta($sql);
  }
}

function drop_coopertion() {
	// global $wpdb;
  // $table_name = $wpdb->prefix . "thetable";

  // $wpdb->query("DROP TABLE IF EXISTS " . $table_name);
}

// 插件激活时,运行回调方法创建数据表, 在WP中添加表
register_activation_hook(__FILE__, 'install_coopertion');
// 插件停用时,运行回调方法删除数据表,删除表
register_deactivation_hook(__FILE__, 'drop_coopertion');

function cooperation_edit_menu()
{
   global $wpdb;
   include 'cooperact.php';
}

function cooperation_admin_actions()
{
  add_menu_page(
   	"合作意向申请", 
   	"申请列表", 
   	'administrator', 
   	__FILE__, 
   	"cooperation_edit_menu",
   	admin_url( '/images/media-button.png', __FILE ) //菜单的 icon 图片的 URL
   );
}

add_action('admin_menu', 'cooperation_admin_actions');



增删改查


function cooperation_list() {
    global $wpdb;
    $cooperations = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "thetable order by id desc limit 10");
   
    if (count($cooperations) <= 0) {
?>
<div id="icon-edit" class="icon32 icon32-posts-post"><br></div>
<h2>申请列表 </h2>
<?php
        echo "暂时没有";
    } else {
?>
<div id="icon-edit" class="icon32 icon32-posts-post"><br></div>
<h2>申请列表 </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="">
        <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>
        </tr>
    </thead>
    <tbody id="the-list">
<?php foreach ($cooperations as $coopera) {?>
            <tr id="post-<?php echo $coopera->id; ?>" class="post-<?php echo $coopera->id; ?> 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="id;?>"></th>
                <td class="post-title page-title column-title">
                 <strong><a class="row-title" href="?page=cooperation%2Fcooperation.php&act=editcooperation&id=<?php echo $coopera->id; ?>" title="title;?>"><?php echo $coopera->name;?></a></strong>
                 <div class="row-actions"><span class="edit">
                 <a href='?page=cooperation%2Fcooperation.php&act=editcooperation&id=<?php echo $coopera->id; ?>'>编辑</a> | </span>
                 <span class="inline hide-if-no-js"><a href='?page=cooperation%2Fcooperation.php&act=deletecooperation&id=<?php echo $coopera->id; ?>'>删除</a> </span>
                 </div>
                </td>
              	<td class="post-title page-title column-title">
              		<?php echo $coopera->phone; ?>
                </td>
                
                <td class="post-title page-title column-title">
                	<?php 
	                	if($coopera->status == 1) {
	                		echo '待处理';
	                	} elseif ($coopera->status == 2) {
	                		echo '处理中';
	                	} elseif ($coopera->status == 3) {
	                		echo '处理完毕';
	                	} else {
	                		echo '垃圾信息';
	                	}
                	?>
                </td>
                <td class="post-title page-title column-title">
                 <?php echo date('Y-m-d H:m', $coopera->time); ?>
                </td>
            </tr>
<?php }//end foreach

}//end if
?>
        </tbody>
</table>
<?php


    if (isset ($_GET['id']) && $_GET['act'] == "editcooperation") {
        $id = $_GET['id'];
        cooperation_edit($id);
    }

    if (isset ($_GET['id']) && $_GET['act'] == "updatecooperation") {
        $id = $_GET['id'];
        cooperation_update($id);
    }
    if (isset ($_GET['id']) && $_GET['act'] == "deletecooperation") {
        $id = $_GET['id'];
        cooperation_delete($id);
    }
} 

function cooperation_delete($id) {
    global $wpdb;
    if (!is_numeric($id)) {
        die("参数id错误");
    }
    $table = $wpdb->prefix . 'thetable';
    $result = $wpdb->query("DELETE FROM $table WHERE id = $id ");
    if ($result == 1) {
        $redirect_url = '?page=cooperation%2Fcooperation.php';
				echo '<script>alert("提交成功!"); window.location.href="'.$redirect_url.'"</script>';
				exit;
    }
}

function cooperation_edit($id) {
    global $wpdb;
    if (!is_numeric($id)) {
        die("参数id错误");
    }
    $coopera = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "thetable" . " WHERE id=$id");
?>
<br/>
<form action="?page=cooperation%2Fcooperation.php&act=updatecooperation&id=<?php echo $coopera->id; ?>" 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 type='text' value='<?php echo $coopera->name; ?>' name='name' > </td>
        </tr>
        <tr>
        	<td>联系方式</td>
        	<td><input type='text' value='<?php echo $coopera->phone; ?>' name='phone' ></td>
        </tr>
        
        <tr>
        	<td>状态</td>
        	<td>
        		<select name="status">
        			<option value="-1">请选择</option>
							<option value="0" <?php if($coopera->status == 0) { echo "selected='selected'"; }?>>垃圾信息</option>
							<option value="1" <?php if($coopera->status == 1) { echo "selected='selected'"; }?>>待处理</option>
							<option value="2" <?php if($coopera->status == 2) { echo "selected='selected'"; }?>>处理中</option>
							<option value="3" <?php if($coopera->status == 3) { echo "selected='selected'"; }?>>处理完毕</option>
						</select>
        	</td>
        </tr>
        <tr>
        	<td></td>
        	<td><input type='submit' value='保存'></td>
        </tr>
       </tbody>
     </table>
   </form>

<?php
}

function cooperation_update($id) {
	global $wpdb;
   if (!is_numeric($id)) {
       die("参数id错误");
   }
	$data = [
		'name' => esc_sql($_POST['name']),
		'phone' => esc_sql($_POST['phone']),
		'status' => intval($_POST['status']) // 0: 垃圾信息, 1:待处理, 2:处理中, 3: 处理完毕
	];
	$condition['id'] = $id;

  $result = $wpdb->update($wpdb->prefix . "thetable", $data, $condition);
  $redirect_url = '?page=cooperation%2Fcooperation.php';
	echo '<script>alert("提交成功!"); window.location.href="'.$redirect_url.'"</script>';
	exit;
}

cooperation_list();

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值