B2主题钩子合集

发布于 更新于
57
B2主题提供了非常丰富的钩子系统,总共包含140+个钩子,分为Action钩子和Filter钩子两大类。这些钩子覆盖了页面结构、内容类型、用户行为、数据处理和系统功能等各个方面,开发者可以通过这些钩子在不修改主题核心文件的情况下,灵活地扩展和自定义B2主题的功能。

一、页面结构相关钩子 (Action Hooks)

1. 头部和页面框架钩子

钩子名称 类型 所在文件 用途说明
b2_header action header.php 在页面头部输出内容
b2_content_before action header.php 在主内容区域之前输出内容
b2_footer_before action footer.php 在页脚之前输出内容
footer-column-1 action footer.php 页脚第一列内容
footer-column-2 action footer.php 页脚第二列内容

2. 首页相关钩子

钩子名称 类型 所在文件 用途说明
b2_index_before action index.php 首页内容之前
b2_index action index.php 首页主要内容区域
b2_index_after action index.php 首页内容之后

二、归档页面钩子 (Archive Pages)

1. 通用归档钩子

钩子名称 类型 所在文件 用途说明
b2_archive_{$tax}_top action archive.php, category.php 归档页面顶部 (动态分类名)
b2_archive_{$tax}_before action archive.php, category.php 归档页面内容之前
b2_archive_{$tax}_content_before action archive.php, category.php 归档页面列表内容之前
b2_archive_{$tax}_content_after action archive.php, category.php 归档页面列表内容之后
b2_archive_{$tax}_after action archive.php, category.php 归档页面内容之后

2. 问答页面钩子

钩子名称 类型 所在文件 用途说明
b2_ask_home_top action archive-ask.php 问答首页顶部
b2_ask_home_before action archive-ask.php 问答首页内容之前
b2_ask_archive_content_before action archive-ask.php 问答归档内容之前
b2_ask_archive_content_after action archive-ask.php 问答归档内容之后

3. 商城页面钩子

钩子名称 类型 所在文件 用途说明
b2_shop_home_top action archive-shop.php 商城首页顶部
b2_shop_home_before action archive-shop.php 商城首页内容之前
b2_shop_archive_content_before action archive-shop.php 商城归档内容之前
b2_shop_archive_content_after action archive-shop.php 商城归档内容之后
b2_shop_home_after action archive-shop.php 商城首页内容之后

三、单页面钩子 (Single Pages)

1. 通用单页面钩子

钩子名称 类型 所在文件 用途说明
b2_single_wrapper_before action single.php, single-ask.php 单页面包装器之前
b2_single_before action single.php, single-ask.php 单页面内容之前
b2_single_content_before action single.php 单页面文章内容之前
b2_single_content_after action single.php 单页面文章内容之后
b2_single_after action single.php, single-ask.php 单页面内容之后
b2_single_wrapper_after action single.php, single-ask.php 单页面包装器之后
b2_comments_after action single.php 评论区域之后

2. 问答单页面钩子

钩子名称 类型 所在文件 用途说明
b2_single_ask_content_before action single-ask.php 问答单页面内容之前
b2_single_ask_content_after action single-ask.php 问答单页面内容之后

3. 文档单页面钩子

钩子名称 类型 所在文件 用途说明
b2_document_wrapper_before action single-document.php 文档页面包装器之前
b2_document_before action single-document.php 文档页面内容之前
b2_document_content_before action single-document.php 文档内容之前
b2_document_content_after action single-document.php 文档内容之后
b2_document_after action single-document.php 文档页面内容之后
b2_document_wrapper_after action single-document.php 文档页面包装器之后

四、商城相关钩子

1. 商城列表钩子

钩子名称 类型 所在文件 用途说明
b2_shop_list_top action TempParts/Shop/shop.php 商城列表顶部
b2_shop_list_before action TempParts/Shop/shop.php 商城列表内容之前
b2_shop_list_content_before action TempParts/Shop/shop.php 商城列表内容区域之前
b2_shop_list_content_after action TempParts/Shop/shop.php 商城列表内容区域之后
b2_shop_list_after action TempParts/Shop/shop.php 商城列表内容之后

2. 商城分类钩子

钩子名称 类型 所在文件 用途说明
b2_shop_category_before action TempParts/Shop/category.php 商城分类页面之前
b2_shop_category_content_before action TempParts/Shop/category.php 商城分类内容之前
b2_shop_category_content_after action TempParts/Shop/category.php 商城分类内容之后
b2_shop_category_after action TempParts/Shop/category.php 商城分类页面之后

3. 商城购买页面钩子

钩子名称 类型 所在文件 用途说明
b2_shop_category_buy_before action TempParts/Shop/shop-buy.php 等 商城购买页面之前
b2_shop_category_buy_content_before action TempParts/Shop/shop-buy.php 等 商城购买内容之前
b2_shop_category_buy_content_after action TempParts/Shop/shop-buy.php 等 商城购买内容之后
b2_shop_category_buy_after action TempParts/Shop/shop-buy.php 等 商城购买页面之后

4. 商城单页面钩子

钩子名称 类型 所在文件 用途说明
b2_single_post_content_before action TempParts/Shop/single-shop.php 商城单页面内容之前
b2_single_post_content_after action TempParts/Shop/single-shop.php 商城单页面内容之后
b2_single_article_after action TempParts/Shop/single-shop.php 商城单页面文章之后

五、用户行为相关钩子 (User Actions)

1. 用户登录注册钩子

钩子名称 类型 所在文件 用途说明
b2_user_login action Modules/Common/Login.php 用户登录时触发
b2_login_out action Modules/Common/Login.php 用户登出时触发
b2_user_regeister action Modules/Common/Login.php, OAuth.php 用户注册时触发
b2_user_social_login action Modules/Common/OAuth.php 用户社交登录时触发
b2_social_binding action Modules/Common/OAuth.php 用户绑定社交账号时触发

2. 问答相关钩子

钩子名称 类型 所在文件 用途说明
b2_user_write_ask action Modules/Common/Ask.php 用户发布问题时触发
b2_user_ask_post_success action Modules/Common/Ask.php 问题发布成功后触发
b2_ask_hotness action Modules/Common/Ask.php 问题热度更新时触发
b2_rebuild_hotness action Modules/Common/Ask.php, Comment.php 重新计算热度时触发

3. 圈子相关钩子

钩子名称 类型 所在文件 用途说明
b2_circle_topic_vote action Modules/Common/Circle.php 圈子话题投票时触发
b2_circle_topic_guess action Modules/Common/Circle.php 圈子话题猜测时触发

4. 评论相关钩子

钩子名称 类型 所在文件 用途说明
b2_comment_vote action Modules/Common/Comment.php 评论投票时触发
b2_comment_text action Modules/Common/Comment.php 评论文本输出时触发

5. 其他用户行为钩子

钩子名称 类型 所在文件 用途说明
b2_send_dmsg_action action Modules/Common/Directmessage.php 发送私信时触发
b2_submit_request action Modules/Common/Document.php 提交请求时触发
b2_submit_newsflashes action Modules/Common/Newsflashes.php 提交动态时触发
b2_use_invitation action Modules/Common/Invitation.php 使用邀请码时触发

六、数据处理相关钩子

1. 积分系统钩子

钩子名称 类型 所在文件 用途说明
b2_gold_update_data_insert action Modules/Common/Gold.php 积分数据插入时触发
b2_gold_update_data_update action Modules/Common/Gold.php 积分数据更新时触发

2. 消息系统钩子

钩子名称 类型 所在文件 用途说明
b2_message_update_data_insert action Modules/Common/Message.php 消息数据插入时触发
b2_message_update_data_update action Modules/Common/Message.php 消息数据更新时触发

3. 订单系统钩子

钩子名称 类型 所在文件 用途说明
b2_before_build_order action Modules/Common/Orders.php 构建订单之前触发

4. 其他数据钩子

钩子名称 类型 所在文件 用途说明
b2_infomation_sticky_expired action Modules/Common/Infomation.php 信息置顶过期时触发

七、过滤器钩子 (Filter Hooks)

1. 核心功能过滤器

钩子名称 类型 所在文件 用途说明
b2_is_page filter functions.php, footer.php 检测页面类型
b2_get_current_user_id filter functions.php 获取当前用户ID
b2_upload_path_arg filter functions.php 上传路径参数
b2_admin_capability filter functions.php 管理员权限检测
b2_get_thumb_action filter functions.php 获取缩略图处理
b2_remove_kh filter functions.php 移除括号内容

2. 数据处理过滤器

钩子名称 类型 所在文件 用途说明
b2_sanitize_data filter Modules/Common/Ask.php等 数据清理处理
b2_text_censor filter Modules/Common/Ask.php等 文本审查过滤
b2_check_repo_before filter 多个模块文件 发布前检查
b2_check_repo_after filter 多个模块文件 发布后处理

3. 圈子相关过滤器

钩子名称 类型 所在文件 用途说明
b2_get_circle_setting_by_id filter functions.php 获取圈子设置
b2_is_user_in_circle filter Modules/Common/Circle.php 检测用户是否在圈子中
b2_get_circle_file_role filter Modules/Common/Circle.php 获取圈子文件权限
b2_get_topic_filter_data filter Modules/Common/Circle.php 获取话题过滤数据

4. 内容处理过滤器

钩子名称 类型 所在文件 用途说明
b2_thumb_webp filter 多个文件 WebP缩略图处理
b2_comment_post_type filter Modules/Common/Circle.php 评论文章类型
b2_get_ask_item filter Modules/Common/Ask.php 获取问答项目数据
b2_get_ask_edit filter Modules/Common/Ask.php 获取问答编辑数据

5. 配置相关过滤器

钩子名称 类型 所在文件 用途说明
b2_order_type filter functions.php 订单类型配置
b2_roles_arg filter functions.php 用户角色参数
b2_weixin_message_templates filter functions.php 微信消息模板
b2_oauth_types_arg filter functions.php OAuth类型参数

6. 安全相关过滤器

钩子名称 类型 所在文件 用途说明
request_uri_items filter functions.php 请求URI项目过滤
query_string_items filter functions.php 查询字符串项目过滤
user_agent_items filter functions.php 用户代理项目过滤

八、使用示例

Action钩子使用示例

// 在页面头部添加自定义内容
add_action('b2_header', function() {
    echo '
自定义头部内容

'; }); // 用户登录时执行自定义操作 add_action('b2_user_login', function($user_id) { // 记录登录日志 error_log("用户 {$user_id} 登录了"); }); // 在单页面内容之前添加广告 add_action('b2_single_content_before', function() { echo '

广告位

'; });

Filter钩子使用示例

// 修改上传路径
add_filter('b2_upload_path_arg', function($path) {
    return $path . '/custom/';
});

// 自定义文本审查
add_filter('b2_text_censor', function($text) {
    // 替换敏感词
    return str_replace('敏感词', '***', $text);
});

// 修改圈子设置
add_filter('b2_get_circle_setting_by_id', function($settings, $circle_id) {
    // 自定义圈子设置
    $settings['custom_option'] = 'custom_value';
    return $settings;
}, 10, 2);
0 赞
0 收藏
分享
0 讨论
反馈
© 版权声明
0 / 600
0 条评论
热门最新
我的会员
加入会员后,您将获得更多权益!