apply_filters( ‘cron_memory_limit’, int|string $filtered_limit )

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

VersionDescription
6.3.0Introduced.

User Contributed Notes

  1. Skip to note 3 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.

  2. Skip to note 4 content

    The cron_memory_limit filter is useful when your scheduled WP-Cron tasks require more memory than the default PHP memory limit allows — especially for operations like processing large product imports, running backups, or handling WooCommerce analytics jobs.

    Example: Set a fixed memory limit for cron tasks only

    add_filter( 'cron_memory_limit', function( $limit ) {
        // Set cron tasks to use 256MB, only if current limit is lower
        if ( intval( $limit ) < 256 ) {
            return '256M';
        }
        return $limit;
    } );

    This ensures memory-intensive cron jobs don’t fail due to insufficient limits while respecting existing high limits set on the server. Always test memory usage to avoid over-allocating on shared hosting environments.

You must log in before being able to contribute a note or feedback.