返回列表 发布新帖
查看: 77|回复: 0

[代码技巧] 子比主题 – 后台关键词搜索管理代码

[复制链接]
SunJu_FaceMall
社区贡献

315

主题

190

回帖

1万

积分

等级头衔
Icon组别 : 管理员
Icon等级 :

积分成就
   钻石 : 524 颗
   贡献 : 1849 点
   金币 : 12 枚
Icon在线时间 : 1297 小时
Icon注册时间 : 2024-11-22
Icon最后登录 : 2025-12-9

荣誉勋章

会员LV.1会员LV.2会员LV.3会员LV.4会员LV.5会员LV.6会员LV.7会员LV.8会员LV.9会员LV.10

风云·优秀版主

飞流名人堂成员

1

实名认证 手机认证 vip vip-year FLLTCN发表于 2025-10-9 23:53:36 | 查看全部 |阅读模式 浙江金华

资源无需等待,交易就趁现在,全面资源整合网络大咖云集,让你轻松玩转互联网!

您需要 登录 才可以下载或查看,没有账号?立即注册

×
这是一款子比主题后台关键词搜索管理的代码,这个代码可以准确清楚的看到用户搜索的关键词,并且管理员可以修改搜索多少次和删掉搜索词,非常实用的一款搜索管理,喜欢的自行部署!

tengfei_down - 2025-10-09T235232.775.webp

代码部署


定位:/wp-content/themes/zibll/func.php,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码放到里面!
  1. function zib_register_keywords_page() {
  2.     add_menu_page(
  3.         '热门搜索关键词',
  4.         '热门关键词管理',
  5.         'manage_options',
  6.         'zib_keywords',
  7.         'zib_keywords_page',
  8.         'dashicons-search',
  9.         2
  10.     );
  11. }
  12. add_action('admin_menu', 'zib_register_keywords_page');
  13. function zib_keywords_page() {
  14.     $filter_types = isset($_GET['filter_types']) ? $_GET['filter_types'] : [];
  15.     if (isset($_POST['submit_add_keyword'])) {
  16.         $new_keyword = sanitize_text_field($_POST['new_keyword']);
  17.         $search_count = intval($_POST['search_count']);
  18.         $keyword_type = sanitize_text_field($_POST['keyword_type']);
  19.         if (!empty($new_keyword)) {
  20.             $keywords = zib_get_option('search_keywords');
  21.             if (!is_array($keywords)) {
  22.                 $keywords = [];
  23.             }
  24.             $keywords[$new_keyword . '&type=' . $keyword_type] = $search_count;
  25.             arsort($keywords);
  26.             zib_update_option('search_keywords', $keywords);
  27.         }
  28.     }
  29.     if (isset($_POST['submit_edit_keyword'])) {
  30.         $old_key = sanitize_text_field($_POST['old_key']);
  31.         $new_keyword = sanitize_text_field($_POST['edit_keyword']);
  32.         $new_count = intval($_POST['edit_count']);
  33.         $new_type = sanitize_text_field($_POST['edit_type']);
  34.         if (!empty($old_key) && !empty($new_keyword)) {
  35.             $keywords = zib_get_option('search_keywords');
  36.             if (is_array($keywords) && isset($keywords[$old_key])) {
  37.                 unset($keywords[$old_key]);
  38.                 $keywords[$new_keyword . '&type=' . $new_type] = $new_count;
  39.                 arsort($keywords);
  40.                 zib_update_option('search_keywords', $keywords);
  41.             }
  42.         }
  43.     }
  44.     if (isset($_POST['bulk_action']) && $_POST['bulk_action'] == 'delete' && isset($_POST['selected_keywords'])) {
  45.         $selected_keywords = $_POST['selected_keywords'];
  46.         $keywords = zib_get_option('search_keywords');
  47.         if (is_array($keywords)) {
  48.             foreach ($selected_keywords as $keyword) {
  49.                 unset($keywords[$keyword]);
  50.             }
  51.             zib_update_option('search_keywords', $keywords);
  52.         }
  53.     }
  54.     $keywords = zib_get_option('search_keywords');
  55.     if (!empty($keywords)) {
  56.         arsort($keywords);
  57.     }
  58.     if (!empty($filter_types)) {
  59.         $filtered_keywords = [];
  60.         foreach ($keywords as $key => $value) {
  61.             $keyword_type = 'unknown';
  62.             if (preg_match('/&type=(post|user|forum|plate)/', $key, $matches)) {
  63.                 $keyword_type = $matches[1];
  64.             }
  65.             if (!in_array($keyword_type, $filter_types) && $keyword_type !== 'unknown') {
  66.                 continue;
  67.             }
  68.             $filtered_keywords[$key] = $value;
  69.         }
  70.         $keywords = $filtered_keywords;
  71.     }
  72.     ?>
  73.     <div class="wrap">
  74.         <h1>热门搜索关键词</h1>
  75.         <div>
  76.             <button id="add_new_keyword" class="button">添加热词</button>
  77.             <button id="bulk_delete" class="button">批量删除</button><br>
  78.             <form method="get" action="<?php echo admin_url('admin.php'); ?>" style="display: inline-block;">
  79.                 <input type="hidden" name="page" value="zib_keywords" />
  80.                 <div style="display: flex; gap: 10px; align-items: center;">
  81.                     <label style="margin-right: 10px;">筛选类型:</label>
  82.                     <label style="margin-right: 10px;">
  83.                         <input type="checkbox" name="filter_types[]" value="post" <?php echo in_array('post', $filter_types) ? 'checked' : ''; ?>>
  84.                         文章
  85.                     </label>
  86.                     <label style="margin-right: 10px;">
  87.                         <input type="checkbox" name="filter_types[]" value="user" <?php echo in_array('user', $filter_types) ? 'checked' : ''; ?>>
  88.                         用户
  89.                     </label>
  90.                     <label style="margin-right: 10px;">
  91.                         <input type="checkbox" name="filter_types[]" value="forum" <?php echo in_array('forum', $filter_types) ? 'checked' : ''; ?>>
  92.                         论坛
  93.                     </label>
  94.                     <label style="margin-right: 10px;">
  95.                         <input type="checkbox" name="filter_types[]" value="plate" <?php echo in_array('plate', $filter_types) ? 'checked' : ''; ?>>
  96.                         板块
  97.                     </label>
  98.                     <input type="submit" value="筛选" class="button" />
  99.                 </div>
  100.             </form>
  101.         </div>
  102.         <form method="post" action="" id="keywords_form">
  103.             <table class="wp-list-table widefat fixed striped">
  104.                 <thead>
  105.                     <tr>
  106.                         <th><input type="checkbox" id="select_all" /></th>
  107.                         <th>#</th>
  108.                         <th>关键词</th>
  109.                         <th>搜索次数</th>
  110.                         <th>搜索类型</th>
  111.                         <th>操作</th>
  112.                     </tr>
  113.                 </thead>
  114.                 <tbody>
  115.                     <?php
  116.                     if (!empty($keywords)) {
  117.                         $i = 1;
  118.                         foreach ($keywords as $key => $value) {
  119.                             $keyword_type = 'unknown';
  120.                             if (preg_match('/&type=(post|user|forum|plate)/', $key, $matches)) {
  121.                                 $keyword_type = $matches[1];
  122.                             }
  123.                             $keyword_base = strtok($key, '&');
  124.                             $type = '其他'; // 默认值
  125.                             switch ($keyword_type) {
  126.                                 case 'post':
  127.                                     $type = '文章';
  128.                                     break;
  129.                                 case 'user':
  130.                                     $type = '用户';
  131.                                     break;
  132.                                 case 'forum':
  133.                                     $type = '论坛';
  134.                                     break;
  135.                                 case 'plate':
  136.                                     $type = '板块';
  137.                                     break;
  138.                                 case 'other':
  139.                                     $type = '其他';
  140.                                     break;
  141.                             }

  142.                             echo "<tr>
  143.                                 <td><input type='checkbox' name='selected_keywords[]' value='$key' /></td> <!-- 复选框 -->
  144.                                 <td>$i</td> <!-- 序号 -->
  145.                                 <td>$keyword_base</td>
  146.                                 <td>$value</td>
  147.                                 <td>$type</td>
  148.                                 <td>
  149.                                     <button type='button' class='button edit_keyword'
  150.                                         data-keyword='$keyword_base'
  151.                                         data-count='$value'
  152.                                         data-type='$keyword_type'
  153.                                         data-fullkey='$key'>编辑</button>
  154.                                     <button class='button delete_keyword' data-keyword='$key'>删除</button>
  155.                                 </td>
  156.                             </tr>";
  157.                             $i++;
  158.                         }
  159.                     }
  160.                     ?>
  161.                 </tbody>
  162.             </table>
  163.         </form>
  164.     </div>
  165.     <div id="add_keyword_modal" style="display: none;" class="modal">
  166.         <div class="modal-content">
  167.             <span id="close_modal" class="close">×</span>
  168.             <h2>添加新热词</h2>
  169.             <form method="post" action="">
  170.                 <p>
  171.                     <label for="new_keyword">关键词:</label>
  172.                     <input type="text" name="new_keyword" required />
  173.                 </p>

  174.                 <p>
  175.                     <label for="search_count">搜索次数:</label>
  176.                     <input type="number" name="search_count" required />
  177.                 </p>

  178.                 <p>
  179.                     <label for="keyword_type">搜索类型:</label>
  180.                     <select name="keyword_type" required>
  181.                         <option value="post">文章</option>
  182.                         <option value="user">用户</option>
  183.                         <option value="forum">论坛</option>
  184.                         <option value="plate">板块</option>
  185.                     </select>
  186.                 </p>

  187.                 <input type="submit" name="submit_add_keyword" value="添加" class="button" />
  188.             </form>
  189.         </div>
  190.     </div>
  191.     <div id="edit_keyword_modal" class="modal">
  192.         <div class="modal-content">
  193.             <span class="close" id="close_edit_modal">×</span>
  194.             <h2>修改热词</h2>
  195.             <form method="post" action="">
  196.                 <input type="hidden" name="old_key" id="edit_old_key">
  197.                
  198.                 <p>
  199.                     <label for="edit_keyword">关键词:</label>
  200.                     <input type="text" name="edit_keyword" id="edit_keyword" required />
  201.                 </p>

  202.                 <p>
  203.                     <label for="edit_count">搜索次数:</label>
  204.                     <input type="number" name="edit_count" id="edit_count" required />
  205.                 </p>

  206.                 <p>
  207.                     <label for="edit_type">搜索类型:</label>
  208.                     <select name="edit_type" id="edit_type" required>
  209.                         <option value="post">文章</option>
  210.                         <option value="user">用户</option>
  211.                         <option value="forum">论坛</option>
  212.                         <option value="plate">板块</option>
  213.                     </select>
  214.                 </p>

  215.                 <input type="submit" name="submit_edit_keyword" value="修改" class="button button-primary" />
  216.             </form>
  217.         </div>
  218.     </div>

  219.     <style>
  220.         .modal {
  221.             display: none;
  222.             position: fixed;
  223.             z-index: 1;
  224.             left: 0;
  225.             top: 0;
  226.             width: 100%;
  227.             height: 100%;
  228.             background-color: rgba(0, 0, 0, 0.4);
  229.             padding-top: 60px;
  230.         }

  231.         .modal-content {
  232.             background-color: #fefefe;
  233.             margin: 5% auto;
  234.             padding: 20px;
  235.             border: 1px solid #888;
  236.             width: 300px;
  237.         }

  238.         .close {
  239.             color: #aaa;
  240.             float: right;
  241.             font-size: 28px;
  242.             font-weight: bold;
  243.         }

  244.         .close:hover,
  245.         .close:focus {
  246.             color: black;
  247.             text-decoration: none;
  248.             cursor: pointer;
  249.         }
  250.     </style>

  251.     <script>
  252.         var modal = document.getElementById("add_keyword_modal");
  253.         var btn = document.getElementById("add_new_keyword");
  254.         var span = document.getElementById("close_modal");
  255.         btn.onclick = function() {
  256.             modal.style.display = "block";
  257.         }
  258.         span.onclick = function() {
  259.             modal.style.display = "none";
  260.         }
  261.         window.onclick = function(event) {
  262.             if (event.target == modal) {
  263.                 modal.style.display = "none";
  264.             }
  265.         }
  266.         document.getElementById("select_all").onclick = function() {
  267.             var checkboxes = document.querySelectorAll("input[name='selected_keywords[]']");
  268.             for (var checkbox of checkboxes) {
  269.                 checkbox.checked = this.checked;
  270.             }
  271.         }
  272.         const deleteButtons = document.querySelectorAll('.delete_keyword');
  273.         deleteButtons.forEach(function(button) {
  274.             button.addEventListener('click', function() {
  275.                 const keyword = button.dataset.keyword;
  276.                 if (confirm('确定删除此关键词吗?')) {
  277.                     const formData = new FormData();
  278.                     formData.append('action', 'delete_keyword');
  279.                     formData.append('keyword', keyword);

  280.                     fetch('<?php echo admin_url('admin-ajax.php'); ?>', {
  281.                         method: 'POST',
  282.                         body: formData
  283.                     })
  284.                     .then(response => response.json())
  285.                     .then(data => {
  286.                         if (data.success) {
  287.                             alert('关键词删除成功');
  288.                             location.reload();
  289.                         } else {
  290.                             alert('删除失败,请重试');
  291.                         }
  292.                     });
  293.                 }
  294.             });
  295.         });
  296.         document.addEventListener('DOMContentLoaded', function() {
  297.             const editModal = document.getElementById("edit_keyword_modal");
  298.             const closeEditBtn = document.getElementById("close_edit_modal");
  299.             const editButtons = document.querySelectorAll('.edit_keyword');
  300.             closeEditBtn.onclick = function() {
  301.                 editModal.style.display = "none";
  302.             }
  303.             editButtons.forEach(function(button) {
  304.                 button.addEventListener('click', function(e) {
  305.                     e.preventDefault();
  306.                     const keyword = this.dataset.keyword;
  307.                     const count = this.dataset.count;
  308.                     const type = this.dataset.type;
  309.                     const fullkey = this.dataset.fullkey;
  310.                     document.getElementById('edit_old_key').value = fullkey;
  311.                     document.getElementById('edit_keyword').value = keyword;
  312.                     document.getElementById('edit_count').value = count;
  313.                     document.getElementById('edit_type').value = type;
  314.                     editModal.style.display = "block";
  315.                 });
  316.             });
  317.             window.onclick = function(event) {
  318.                 if (event.target == editModal) {
  319.                     editModal.style.display = "none";
  320.                 }
  321.                 if (event.target == modal) {
  322.                     modal.style.display = "none";
  323.                 }
  324.             }
  325.         });
  326.         document.getElementById('bulk_delete').addEventListener('click', function(e) {
  327.             e.preventDefault();
  328.             const selectedBoxes = document.querySelectorAll("input[name='selected_keywords[]']:checked");
  329.             if (selectedBoxes.length === 0) {
  330.                 alert('请至少选择一个关键词');
  331.                 return;
  332.             }
  333.             if (confirm('确定要删除选中的 ' + selectedBoxes.length + ' 个关键词吗?')) {
  334.                 const formData = new FormData();
  335.                 formData.append('action', 'bulk_delete_keywords');
  336.                
  337.                 selectedBoxes.forEach(box => {
  338.                     formData.append('keywords[]', box.value);
  339.                 });
  340.                 fetch('<?php echo admin_url('admin-ajax.php'); ?>', {
  341.                     method: 'POST',
  342.                     body: formData
  343.                 })
  344.                 .then(response => response.json())
  345.                 .then(data => {
  346.                     if (data.success) {
  347.                         alert('成功删除 ' + selectedBoxes.length + ' 个关键词');
  348.                         location.reload();
  349.                     } else {
  350.                         alert('删除失败,请重试');
  351.                     }
  352.                 });
  353.             }
  354.         });
  355.     </script>
  356.     <?php
  357. }
  358. add_action('wp_ajax_delete_keyword', 'zib_delete_keyword');
  359. function zib_delete_keyword() {
  360.     if (!isset($_POST['keyword']) || !current_user_can('manage_options')) {
  361.         wp_send_json_error(['message' => '权限不足或缺少关键词']);
  362.     }
  363.     $keyword = sanitize_text_field($_POST['keyword']);
  364.     $keywords = zib_get_option('search_keywords');
  365.     if (is_array($keywords) && isset($keywords[$keyword])) {
  366.         unset($keywords[$keyword]);
  367.         zib_update_option('search_keywords', $keywords);
  368.         wp_send_json_success();
  369.     }
  370.     wp_send_json_error(['message' => '关键词不存在']);
  371. }

  372. add_action('wp_ajax_bulk_delete_keywords', 'zib_bulk_delete_keywords');
  373. function zib_bulk_delete_keywords() {
  374.     if (!current_user_can('manage_options')) {
  375.         wp_send_json_error(['message' => '权限不足']);
  376.     }
  377.     if (!isset($_POST['keywords']) || !is_array($_POST['keywords'])) {
  378.         wp_send_json_error(['message' => '未选择关键词']);
  379.     }
  380.     $keywords = zib_get_option('search_keywords');
  381.     if (!is_array($keywords)) {
  382.         wp_send_json_error(['message' => '关键词列表无效']);
  383.     }
  384.     foreach ($_POST['keywords'] as $keyword) {
  385.         $keyword = sanitize_text_field($keyword);
  386.         if (isset($keywords[$keyword])) {
  387.             unset($keywords[$keyword]);
  388.         }
  389.     }
  390.     zib_update_option('search_keywords', $keywords);
  391.     wp_send_json_success();
  392. }
复制代码


本帖被以下淘专辑推荐:

路虽远,行则将至;事虽难,做则必成。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

飞流广播+ 发布

系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
10-30 17:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
10-09 09:07
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-24 09:00
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-11 11:40
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-02 09:17
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-27 08:56
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-20 15:12
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-03 10:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
06-30 08:57
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
06-18 09:14
系统消息:清风网络已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-11 09:40
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-10 09:31
系统消息:IXM77777已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 13:44
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 09:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-09 08:52
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-08 09:24
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-07 09:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
02-27 09:35
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-26 09:06
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-25 08:49
站内通告

提供资源交易、信息共享、靓号交流、技术变现、学习问答、兴趣娱乐等全面服务。

1.丰富功能系统,扩展社区特色玩法,打造最好的互联网聚集圈子。

2.准确信息真实交易,安全快捷又方便,让虚拟交易面对面。

3. 天上不会掉馅饼,话术骗术迷人心,切勿脱离平台线下交易,被骗与平台无关!

4. 欺诈骗钱,违规违法将视情受到警告&禁言&封号甚至检举至👮🏻‍♀️处理!

官方Q群:123129钉推群:BAYR2383 站长QQ:3220000000

投诉/建议/商务合作联系

fl@fllt.cn

严禁私下交易,被骗与本站无关。
违反交易细则,取证立查严惩。
  • 钉钉新帖推送群
  • 官方交流QQ群
  • 站长唯一微信号

👮曝光Ta|🧿小黑屋|📴手机页|飞流网 ( 渝ICP备2025054677号-1|电信增值许可 渝B2-20250789 )|网站地图

GMT+8, 2025-12-10 12:52 , Processed in 0.100638 second(s), 61 queries, MemCached On , Gzip On.

Based on XJ-TX X3.5 Licensed

飞流论坛 HanAnalytics icp Astro vhAstro-Theme

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表