customize_render_partials_response
概述
customize_render_partials_response 是一个 WordPress 过滤器,用于在渲染部分内容时修改响应数据。插件可以利用此过滤器注入脚本和样式依赖,这些数据通过 JS 事件传递给客户端,以便在 DOM 中注入未排队的资源。
关键要点
- 过滤器允许插件在响应中注入 $scripts 和 $styles 依赖项,用于支持部分内容的渲染。
- 响应数据通过 render-partials-response JS 事件提供给客户端,客户端可以据此在 DOM 中注入脚本和样式。
- 插件需注意避免注入使用 document.write() 的脚本,以防止页面破坏,并应覆盖相关函数或避免注入。
- 参数包括 $response(响应数组)、$refresh(选择性刷新组件)和 $partials(部分内容的上下文数据数组)。
- 此过滤器在 WP_Customize_Selective_Refresh::handle_render_partials_request() 中使用,处理 Ajax 请求以返回渲染的部分内容。
注意事项
- 插件应意识到 $scripts 和 $styles 可能最终默认包含在响应中,需相应调整实现。
- 确保错误处理,特别是在 WP_DEBUG_DISPLAY 启用时,响应中包含错误列表。
Filters the response from rendering the partials.
Description
Plugins may use this filter to inject $scripts and $styles, which are dependencies for the partials being rendered. The response data will be available to the client via the render-partials-response JS event, so the client can then inject the scripts and styles into the DOM if they have not already been enqueued there.
If plugins do this, they’ll need to take care for any scripts that do document.write() and make sure that these are not injected, or else to override the function to no-op, or else the page will be destroyed.
Plugins should be aware that $scripts and $styles may eventually be included by default in the response.
Parameters
$responsearray-
Response.
contentsarrayAssociative array mapping a partial ID its corresponding array of contents for the containers requested.errorsarrayList of errors triggered during rendering of partials, ifWP_DEBUG_DISPLAYis enabled.
$refreshWP_Customize_Selective_Refresh-
Selective refresh component.
$partialsarray-
Placements’ context data for the partials rendered in the request.
The array is keyed by partial ID, with each item being an array of the placements’ context data.
Source
$response = apply_filters( 'customize_render_partials_response', $response, $this, $partials );
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |