wordpress在线留言

更新时间:2023-05-11 00:05:38 阅读: 评论:0

wordpress在线留⾔
⽹上翻了下,⼤部分留⾔都是⽤评论做的,想了想,还是⾃⼰写个吧。因为我的留⾔就只有⾸页下⾯有⼀个留⾔表单
1.⾸先前端⾸页加⼊表单
<div class="contact_w3agile" id="contact">
<div class="container wow fadeInUp animated animated" data-wow-delay=".5s" >  <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(ist($_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->inrt($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.后台管理
之前有写过⼴告管理功能,做了通⽤后台页⾯,直接拿来⽤就可以了
advert_manager.php
$this->sonliss_menu_page('留⾔管理', '留⾔管理', 'feedbacks', 'sonliss_feedback_page');
/**
* 留⾔页⾯
*/
public function sonliss_feedback_page()
{
$action = ist($_GET['action']) ? $_GET['action'] : '';
$feedback = new Feedback();
switch($action)
{
ca 'edit':
$feedback->edit_feedback_form($_GET['id']);
break;
ca 'update':
$feedback->feedback_save($_POST['id']);
$redirect_url = admin_url('admin.php?page=feedback');
echo "<script>alert('提交成功');window.location.href='" . $redirect_url . "';</script>";        exit;
ca 'delete':
$feedback->feedback_delete($_GET['id']);
$redirect_url = admin_url('admin.php?page=feedback');
echo "<script>alert('提交成功');window.location.href='" . $redirect_url . "';</script>";        exit;
ca '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>";        } el {
echo "<script>alert('提交失败');window.location.href='" . $redirect_url . "';</script>";        }
exit;
default:
$pagenum = ist($_GET['pagenum']) ? intval($_GET['pagenum']) : 1;
if (ist($_GET['orderby']) && ist($_GET['order']))
{
$feedback->feedback_list($pagenum, $_GET['orderby'], $_GET['order']);
} el
{
$feedback->feedback_list($pagenum);
}
break;
}
}
feedback.php
require_once( dirname(__FILE__) . '\sonliss.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');
$offt = ( $page - 1 ) * $limit;
$total_pages = ceil( $total_data / $limit );
$feedbacks = $this->get_html_list('feedbacks', 'id, name, email, message, created_at', $orderby, $order, $offt, $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 = ist( $_POST['name'] ) ? wp_unslash( $_POST['name'] ) : '';
$email = ist( $_POST['email'] ) ? wp_unslash( $_POST['email'] ) : '';
$message = ist( $_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);
}
}

本文发布于:2023-05-11 00:05:38,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/881130.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:表单   管理   添加   提交   失败
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图