wordpress 在线留言

网上翻了下,大部分留言都是用评论做的,想了想,还是自己写个吧。因为我的留言就只有首页下面有一个留言表单

1.首先前端首页加入表单

<div class="contact_w3agile" id="contact">
	<div class="container wow fadeInUp animated animated" data-wow-delay=".5s" style="visibility: visible; animation-delay: 0.5s; animation-name: fadeInUp;">
		<h2 class="title">联系我们</h2>
		<form name="theForm" action="/feedbacks" method="post">
			<input type="text" value="" name="name" placeholder="称呼" required />
			<input type="email address" value="" name="email" placeholder="邮箱" />
			<textarea name="message" required placeholder="你的表达" ></textarea>
			<div class="con-form text-center">
				<input type="submit" value="提交">
			</div>
		</form>
	</div>
</div>

2.在自己的主题里的functions.php中添加路由

add_rewrite_rule('feedbacks', 'index.php?pagename=feedbacks', 'top');

3.在主题里添加feedbacks.php,用于接收表单数据

if(isset($_POST['name']) && !empty($_POST['name']))
{
	$name = strip_tags($_POST['name']);
	$email = strip_tags($_POST['email']);
	$message = strip_tags($_POST['message']);

	global $wpdb;
	$save_table = $wpdb->prefix . 'feedbacks';
	$data = [
		'name' => $name,
		'email' => $email,
		'message' => $message,
		'created_at' => date_i18n('Y-m-d H:m:s'),
	];
	$wpdb->insert($save_table, $data);
	$redirect_url = home_url();
	echo "<script>alert('提交成功');window.location.href='" . $redirect_url . "';</script>";
	exit;
}

4.加载feedbacks.php,在functions.php中加载

function feedbacks_template() {
  $feedbacks_page = get_query_var('pagename');
  if ($feedbacks_page == 'feedbacks')
  {
    $template = get_template_directory() . '/feedbacks.php';
    include($template);
    exit;
  }
}
add_action( 'template_redirect', 'feedbacks_template' );

这样就可以在首页提交留言了,

5.后台管理

之前有写过广告管理功能,做了通用后台页面,直接拿来用就可以了

https://blog.csdn.net/tang05709/article/details/88837920

advert_manager.php

$this->sonliss_menu_page('留言管理', '留言管理', 'feedbacks', 'sonliss_feedback_page');
/**
   * 留言页面
   */
  public function sonliss_feedback_page()
  {
    $action = isset($_GET['action']) ? $_GET['action'] : '';
    $feedback = new Feedback();
    switch($action)
    {
      case 'edit':
        $feedback->edit_feedback_form($_GET['id']);
        break;
      case 'update':
        $feedback->feedback_save($_POST['id']);
        $redirect_url = admin_url('admin.php?page=feedback');
        echo "<script>alert('提交成功');window.location.href='" . $redirect_url . "';</script>";
        exit;
      case 'delete':
        $feedback->feedback_delete($_GET['id']);
        $redirect_url = admin_url('admin.php?page=feedback');
        echo "<script>alert('提交成功');window.location.href='" . $redirect_url . "';</script>";
        exit;
      case 'batch_delete':
        $ids = $_POST['column'];
        $redirect_url = admin_url('admin.php?page=feedback');
        if (count($ids) > 0)
        {
          foreach($ids as $id)
          {
            $feedback->feedback_delete($id);
          }
          echo "<script>alert('提交成功');window.location.href='" . $redirect_url . "';</script>";
        } else {
          echo "<script>alert('提交失败');window.location.href='" . $redirect_url . "';</script>";
        }
        exit;
      default: 
        $pagenum = isset($_GET['pagenum']) ? intval($_GET['pagenum']) : 1;
        if (isset($_GET['orderby']) && isset($_GET['order']))
        {
          $feedback->feedback_list($pagenum, $_GET['orderby'], $_GET['order']);
        } else 
        {
          $feedback->feedback_list($pagenum);
        }
        break;
    }
  }

feedback.php

require_once( dirname(__FILE__) . '\sonliss.php' );
class Feedback extends Sonliss
{
  /**
   * 留言列表
   */
  public function feedback_list($page = 1, $orderby = 'id', $order = 'desc')
  {
    $new_order = 'asc';
    if ($order == 'asc')
    {
      $new_order = 'desc';
    }
    $table_header = [
      ['title' => '', 'check_column' => true, 'sort_column' => ['id', $new_order]],
      ['title' => '联系人', 'column_primary' => true, 'sort_column' => ['name', $new_order]],
      ['title' => '邮箱'],
      ['title' => '信息'],
      ['title' => '日期']
    ];
    // 分页
    $limit = 20;
    $total_data = $this->get_total_data('feedbacks');
    $offset = ( $page - 1 ) * $limit;
    $total_pages = ceil( $total_data / $limit );
    $feedbacks = $this->get_html_list('feedbacks', 'id, name, email, message, created_at', $orderby, $order, $offset, $limit);
    $table_body = [];
    foreach($feedbacks as $key => $val)
    {
      $table_body[$val->id] = [
        ['value' => $val->id, 'check_column' => true],
        ['value' => $val->name, 'column_primary' => true],
        ['value' => $val->email],
        ['value' => $val->message],
        ['value' => $val->created_at]
      ];
    }
    $title = '留言';
    $pagination = [
      "current" => $page,
      'total' => $total_pages
    ];
    echo $this->html_list($title, 'feedback', $table_header, $table_body, $total_data, $pagination);
  }

  /**
   * 留言修改表单
   */
  public function edit_feedback_form($id)
  {
    $row = $this->get_form_data('feedbacks', $id);
    $id_input = $this->create_input_tr('hidden', 'id', '', $id);
    $name = $this->create_input_tr('text', 'name', '留言', $row->name, [], ['aria-required' => true]);
    $email = $this->create_input_tr('text', 'email', 'email', $row->email);
    $message = $this->create_input_tr('textarea', 'message', '信息', $row->message);
    $input_lists = $id_input . $name . $email . $message;
    $action =  admin_url( 'admin.php?page=feedback&action=update&id=' . $id );
    $html = $this->html_form($action, '修改留言', 'feedback', $input_lists);
    echo $html;
  }

  /**
   * 留言广告
   */
  public function feedback_save($id = 0)
  {
    $table = 'feedbacks';
    $name = isset( $_POST['name'] ) ? wp_unslash( $_POST['name'] ) : '';
    $email = isset( $_POST['email'] ) ? wp_unslash( $_POST['email'] ) : '';
    $message = isset( $_POST['message'] ) ? wp_unslash( $_POST['message'] ) : '';
    $data = ['name' => $name, 'email' => $email, 'message' => $message];
    $where = [];
    if($id > 0)
    {
      $where = ['id' => $id];
    } 
    return $this->html_save($table, $data, $where);
  }

  /**
   * 留言
   */
  public function feedback_delete($id)
  {
    $table = 'feedbacks';
    return $this->delete_form_data($table, $id);
  }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值