钩子文档

pre_get_site_by_path

💡 云策文档标注

概述

pre_get_site_by_path 是一个 WordPress 过滤器,用于在根据域名和路径确定站点时,允许开发者自定义或优化逻辑。它可以在默认检索站点前进行干预,返回站点对象、false 或 null 来控制流程。

关键要点

  • 这是一个过滤器,钩子名为 'pre_get_site_by_path',用于在 get_site_by_path() 函数执行前处理站点检索。
  • 参数包括 $site(初始为 null)、$domain(请求的域名)、$path(请求的完整路径)、$segments(建议的路径段数,默认为 null)和 $paths(基于 $path 和 $segments 生成的搜索路径数组)。
  • 返回值:返回 null 以跳过自定义逻辑并继续默认检索;返回 false 表示未找到站点;返回 WP_Site 对象表示找到的站点。
  • 首次引入于 WordPress 3.9.0 版本,主要用于多站点网络环境。

代码示例

$pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );

注意事项

  • 此过滤器允许开发者实现更高效的站点查找逻辑,例如基于缓存或自定义数据库查询。
  • 返回 null 时,WordPress 会继续执行默认的 get_site_by_path() 逻辑;返回 false 或 WP_Site 对象将直接终止默认检索。
  • 相关函数 get_site_by_path() 位于 wp-includes/ms-load.php 文件中,用于检索最匹配的站点对象。

📄 原文内容

Determines a site by its domain and path.

Description

This allows one to short-circuit the default logic, perhaps by replacing it with a routine that is more optimal for your setup.

Return null to avoid the short-circuit. Return false if no site can be found at the requested domain and path. Otherwise, return a site object.

Parameters

$sitenull|false|WP_Site
Site value to return by path. Default null to continue retrieving the site.
$domainstring
The requested domain.
$pathstring
The requested path, in full.
$segmentsint|null
The suggested number of paths to consult.
Default null, meaning the entire path was to be consulted.
$pathsstring[]
The paths to search for, based on $path and $segments.

Source

$pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );

Changelog

Version Description
3.9.0 Introduced.