钩子文档

{$context}_memory_limit

💡 云策文档标注

概述

本文档介绍了 WordPress 中的 {$context}_memory_limit 过滤器,用于动态调整特定上下文的内存限制。该过滤器允许插件或主题根据自定义上下文来增加内存分配,适用于内存密集型操作。

关键要点

  • 过滤器名称 {$context}_memory_limit 中的 $context 是动态部分,代表任意上下文,使开发者能定义自己的内存限制场景。
  • 参数 $filtered_limit 接受整数(字节)或简写字符串(如 '256M'),默认值为 WP_MAX_MEMORY_LIMIT 或 php.ini 中的 memory_limit 的较高者。
  • 此过滤器通过 apply_filters 调用,常用于 wp_raise_memory_limit() 函数中,以提升 PHP 内存限制。
  • 自 WordPress 4.6.0 版本引入,是内存管理的重要工具。

代码示例

$filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );

注意事项

  • 使用时应确保 $context 参数明确,避免与其他过滤器冲突。
  • 内存限制设置需考虑服务器配置,避免设置过高导致性能问题。

📄 原文内容

Filters the memory limit allocated for an arbitrary context.

Description

The dynamic portion of the hook name, $context, refers to an arbitrary context passed on calling the function. This allows for plugins to define their own contexts for raising the memory limit.

Parameters

$filtered_limitint|string
Maximum memory limit to allocate for this context.
Default WP_MAX_MEMORY_LIMITor the original php.inimemory_limit`, whichever is higher. Accepts an integer (bytes), or a shorthand string notation, such as '256M'.

Source

$filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );

Changelog

Version Description
4.6.0 Introduced.