WordPress 网站搜索替换为 Bing(必应)或者 Google(谷歌)站内搜索

文章首发于公众号【小白书签】:https://mp.weixin.qq.com/s/JzsT1TDNhaG9MEHrWKU2UQ

WordPress 自带的站内搜索功能比较弱,也很占用服务器资源,有些网站会利用搜索引擎来替代。那么,今天就给大家分享下,可将 WordPress 搜索替换为 Bing 或 Google 站内搜索的代码.

Bing 必应

function bing_search_enqueue_script() {
    ?>
    <script type="text/javascript">
      document.addEventListener('DOMContentLoaded', function() { // DOM完全加载后执行
        var form = document.querySelector('.search-form'); // 选择搜索表单元素
        var searchInput = form.querySelector('.search-input'); // 选择搜索输入框

function isMobile() {
            // 判断当前设备是否为移动设备
return /Mobi|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
        }

        form.addEventListener('submit', function(event) { // 表单提交事件监听器
          event.preventDefault(); // 阻止默认提交行为

          var query = searchInput.value.trim(); // 获取并去除输入的查询字符串的首尾空白
          var siteRestriction = 'site:网站域名'; // 指定站点限制

if (!query.includes(siteRestriction)) {
            // 如果查询字符串中不包含站点限制,添加站点限制
            query = siteRestriction + ' ' + query;
          }

          var bingUrl; // 定义Bing搜索的URL
          var hostname = window.location.hostname; // 获取当前站点的主机名
if (hostname.endsWith('.cn')) {
            // 如果主机名以.cn结尾,则使用中国版的Bing
            bingUrl = 'https://cn.bing.com/search?q=' + encodeURIComponent(query);
          } else {
            // 否则使用国际版的Bing
            bingUrl = 'https://www.bing.com/search?q=' + encodeURIComponent(query);
          }

if (isMobile()) {
            // 如果是移动设备,直接跳转到Bing搜索结果页面
            window.location.href = bingUrl;
          } else {
            // 如果是桌面设备,使用新标签页打开Bing搜索结果
            var link = document.createElement('a'); // 创建一个<a>元素
            link.href = bingUrl; // 设置<a>元素的href为Bing URL
            link.target = '_blank'; // 在新标签页中打开
            link.rel = 'noopener noreferrer nofollow'; // 设置安全和SEO相关属性
            document.body.appendChild(link); // 将<a>元素添加到文档
            link.click(); // 触发点击事件打开链接
            document.body.removeChild(link); // 从文档中移除<a>元素
          }
        });
      });
    </script>
    <?php
}
add_action('wp_footer', 'bing_search_enqueue_script'); // 将脚本添加到WordPress页脚中

总体功能描述:

  1. 挂载时机:在页面加载完成后执行。
  2. 主要功能:捕捉搜索表单提交事件,并将用户的搜索查询重定向到 Bing 搜索引擎,同时限定搜索结果只在特定网站上。
  3. 特殊处理:根据访问设备(移动设备或桌面设备)决定搜索结果页面的打开方式(直接跳转或在新标签页中打开)。

提醒事项

  • 代码确保用户搜索被限制在特定网站( site:网站域名)内,需注意要替换为实际的”网站域名“。
  • 使用了 rel="noopener noreferrer nofollow" 属性,防止点击劫持和传递不必要的权重。
  • add_action('wp_footer', 'bing_search_enqueue_script'); 确保了这个脚本被添加到WordPress页面的页脚中。

Google 谷歌

function google_search_enqueue_script() {
    ?>
    <script type="text/javascript">
      document.addEventListener('DOMContentLoaded', function() { // 在DOM完全加载后执行
        var form = document.querySelector('.search-form'); // 选择搜索表单元素
        var searchInput = form.querySelector('.search-input'); // 选择搜索输入框

function isMobile() {
            // 判断当前设备是否为移动设备
return /Mobi|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
        }

        form.addEventListener('submit', function(event) { // 添加表单提交事件的监听器
          event.preventDefault(); // 阻止默认的表单提交行为

          var query = searchInput.value.trim(); // 获取并去除输入的查询字符串的首尾空白
          var siteRestriction = 'site:网站域名'; // 指定站点限制,注意替换为实际域名

if (!query.includes(siteRestriction)) {
            // 如果查询字符串中不包含站点限制,添加站点限制
            query = siteRestriction + ' ' + query;
          }

          var googleUrl = 'https://www.google.com/search?q=' + encodeURIComponent(query); // 构建Google搜索的URL

if (isMobile()) {
            // 如果是移动设备,直接跳转到Google搜索结果页面
            window.location.href = googleUrl;
          } else {
            // 如果是桌面设备,在新标签页中打开Google搜索结果
            var link = document.createElement('a'); // 创建一个<a>元素
            link.href = googleUrl; // 设置<a>元素的href为Google URL
            link.target = '_blank'; // 在新标签页中打开
            link.rel = 'noopener noreferrer nofollow'; // 设置安全和SEO相关属性
            document.body.appendChild(link); // 将<a>元素添加到文档
            link.click(); // 触发点击事件打开链接
            document.body.removeChild(link); // 从文档中移除<a>元素
          }
        });
      });
    </script>
    <?php
}
add_action('wp_footer', 'google_search_enqueue_script'); // 将脚本添加到WordPress页脚中

代码功能跟 Bing 部分大同小异,不再赘述。需要注意的是,其中 Google 搜索地址部分https://www.google.com可换成谷歌的一些镜像站点地址,以便国内能够正常访问。当然,如无特殊要求,国内建议首选 Bing 站内搜索。

0

评论0

没有账号?注册  忘记密码?

社交账号快速登录

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码