如何为 SEO URL 请求添加自定义 JSON-LD Schema 标记
问题
HUSKY 过滤器的“SEO URL 请求”字段 <script> tags for security reasons, preventing direct insertion of JSON-LD structured data (schema markup) for Google rich results.
解决方案概述
不要直接将完整的 JSON-LD 代码插入 SEO 文本字段,请使用 schema keys 在 HUSKY 管理后台,并将实际的 JSON-LD 代码存储在单独的文件或 functions.php 中。
方法 1:静态 JSON 文件(推荐用于简单模式)
步骤 1: 创建 schema 文件文件夹
- Navigate to
/wp-content/uploads/ - 创建新文件夹:
json-ld-schemas
步骤 2: 创建 JSON 模式文件
- 创建文件,如
category1.json,products.json, etc. - 在每个文件中,添加纯 JSON-LD 代码(不含
<script>标签):{ "@context": "https://schema.org", "@type": "CollectionPage", "name": "Product Category", "description": "Category description" }
步骤 3: 将此代码添加到 functions.php:
add_action('init', function () {
$url_request = WOOF_EXT::$includes['applications']['url_request'];
remove_filter('woocommerce_after_shop_loop', array($url_request->seo, 'add_seo_text'), 99999);
add_filter('woocommerce_after_shop_loop', function () use ($url_request) {
$rule = $url_request->seo->check_search_rules();
if (!isset($rule['text']) || !$url_request->seo->do_index()) {
return;
}
$schema_key = apply_filters('woof_seo_text', $url_request->seo->replace_vars($rule['text'], $url_request->seo->get_current_replace_vars()));
if (!empty($schema_key)) {
$upload_dir = wp_upload_dir();
$schemas_dir = $upload_dir['basedir'] . '/json-ld-schemas/';
$schema_file = $schemas_dir . $schema_key . '.json';
if (file_exists($schema_file)) {
$json_content = file_get_contents($schema_file);
echo '<div class="woof_seo_text"><script type="application/ld+json">' . $json_content . "</script></div>\r\n";
}
}
}, 99999);
}, 1);
步骤 4: 在 HUSKY 管理员(SEO URL 请求 → SEO 文本字段)
- Enter only the schema key (例如,
category1) - Do NOT enter the full JSON-LD code
方法 2:动态 PHP 文件(适用于动态模式)
步骤 1: 在…中创建 PHP schema 文件 /wp-content/uploads/json-ld-schemas/
示例: category-dynamic.php
<?php
global $wp_query;
$term = get_queried_object();
$category_name = $term ? $term->name : 'Products';
$category_url = get_term_link($term);
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'CollectionPage',
'name' => $category_name,
'url' => $category_url,
'description' => term_description($term->term_id),
'breadcrumb' => array(
'@type' => 'BreadcrumbList',
'itemListElement' => array(
array(
'@type' => 'ListItem',
'position' => 1,
'name' => 'Home',
'item' => home_url()
),
array(
'@type' => 'ListItem',
'position' => 2,
'name' => $category_name,
'item' => $category_url
)
)
)
);
return $schema;
步骤 2: 将此代码添加到 functions.php:
add_action('init', function () {
$url_request = WOOF_EXT::$includes['applications']['url_request'];
remove_filter('woocommerce_after_shop_loop', array($url_request->seo, 'add_seo_text'), 99999);
add_filter('woocommerce_after_shop_loop', function () use ($url_request) {
$rule = $url_request->seo->check_search_rules();
if (!isset($rule['text']) || !$url_request->seo->do_index()) {
return;
}
$schema_key = apply_filters('woof_seo_text', $url_request->seo->replace_vars($rule['text'], $url_request->seo->get_current_replace_vars()));
if (!empty($schema_key)) {
$upload_dir = wp_upload_dir();
$schemas_dir = $upload_dir['basedir'] . '/json-ld-schemas/';
$schema_file = $schemas_dir . $schema_key . '.php';
if (file_exists($schema_file)) {
$schema_data = include($schema_file);
$json_output = json_encode($schema_data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
echo '<div class="woof_seo_text"><script type="application/ld+json">' . $json_output . "</script></div>\r\n";
}
}
}, 99999);
}, 1);
Usage Example
在 HUSKY 管理员中:
- 转到“SEO URL 请求”设置
- 为特定的过滤器组合创建规则
- 在“SEO 文本”字段中,只输入:
category-dynamic(或任何键名) - 保存
结果:
- 模式文件
/wp-content/uploads/json-ld-schemas/category-dynamic.php将会加载 - JSON-LD 将在页面源代码中正确呈现
- Google can read and validate the structured data

重要提示
- 仅使用键 在 HUSKY 管理员 SEO 文本字段中(例如,
products,category1) - 永远不要粘贴 直接在 HUSKY 后台插入完整的 JSON-LD 代码
- 对于静态模式 → 使用
.jsonfiles - 对于动态模式 → 使用
.php返回数组的文件 - 更改后清除缓存
验证
After implementation, validate your schema:
- 在您的网站上访问过滤后的页面
- 复制页面 URL
- 测试地址: https://search.google.com/test/rich-results
- 检查错误