钩子文档

cron_memory_limit

💡 云策文档标注

概述

cron_memory_limit 是一个 WordPress 过滤器,用于调整 WP-Cron 事件处理时分配的内存限制。它允许开发者自定义内存上限,以优化后台任务性能。

关键要点

  • 过滤器名称:cron_memory_limit,用于修改 WP-Cron 的内存限制。
  • 参数:$filtered_limit,可以是整数(字节)或简写字符串(如 '256M'),默认值为 WP_MAX_MEMORY_LIMIT 或 php.ini 的 memory_limit 中的较高者。
  • 用法:通过 add_filter 钩子添加自定义回调函数来调整内存限制。

代码示例

// 回调函数调整 cron 任务内存限制
function wpdocs_cron_memory_limit( $limit ) {
    // 根据需求修改内存限制,例如增加 128M
    if ( is_numeric( $limit ) ) {
        $new_limit = $limit + 128;
        return $new_limit;
    }
    return $limit;
}

// 将自定义函数钩入 'cron_memory_limit' 过滤器
add_filter( 'cron_memory_limit', 'wpdocs_cron_memory_limit' );

// 在需要获取调整后内存限制的代码中调用
$memory_limit = apply_filters( 'cron_memory_limit', ini_get( 'memory_limit' ) );
echo 'Adjusted memory limit for cron tasks: ' . $memory_limit;

注意事项

  • 此过滤器从 WordPress 6.3.0 版本开始引入。
  • 相关函数:wp_raise_memory_limit() 可用于提升内存密集型进程的 PHP 内存限制。
  • 确保回调函数正确处理参数类型,避免内存设置错误。

📄 原文内容

Filters the memory limit allocated for WP-Cron event processing.

Parameters

$filtered_limitint|string
Maximum memory limit to allocate for WP-Cron.
Default WP_MAX_MEMORY_LIMIT or the original php.ini memory_limit, whichever is higher.
Accepts an integer (bytes), or a shorthand string notation, such as '256M'.

Source

$filtered_limit = apply_filters( 'cron_memory_limit', $filtered_limit );

Changelog

Version Description
6.3.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    // This is your callback function that adjusts the memory limit for cron tasks
    function wpdocs_cron_memory_limit( $limit ) {
        // You can modify the memory limit based on your requirements
        // For this example, let's increase the limit by 128M
        if ( is_numeric( $limit ) ) {
            $new_limit = $limit + 128;
            return $new_limit;
        }
    
        return $limit;
    }
    
    // Hook your custom function to the 'cron_memory_limit' filter
    add_filter( 'cron_memory_limit', 'wpdocs_cron_memory_limit' );
    
    // Somewhere in your code where you need to retrieve the adjusted memory limit
    $memory_limit = apply_filters( 'cron_memory_limit', ini_get( 'memory_limit' ) );
    
    echo 'Adjusted memory limit for cron tasks: ' . $memory_limit;

    In this example, we’re assuming you are working within a WordPress environment and you want to adjust the memory limit for cron tasks. The apply_filters function allows you to apply a series of filters to a specific value, in this case, the memory limit. The custom_cron_memory_limit function acts as a filter callback, adjusting the memory limit by adding 128M to it.

    When you call apply_filters('cron_memory_limit', ini_get('memory_limit')) WordPress will execute your custom function and pass the current memory limit value as an argument. Your function will then modify the limit and return the adjusted value, which will be used wherever the apply_filters function is called. Finally, the adjusted memory limit is echoed out in the example.