wppaste
WordPress

wp_raise_memory_limit( string $context = 'admin' ): int|string|false

Since
4.6.0
Source
wp-includes/functions.php:7842
Attempts to raise the PHP memory limit for memory intensive processes.

Description

Only allows raising the existing limit and prevents lowering it.

Compatibility

WordPress
since 4.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$contextstringoptional
Context in which the function is called. Accepts either 'admin', 'image', 'cron', or an arbitrary other context. If an arbitrary context is passed, the similarly arbitrary '$context_memory_limit' filter will be invoked. Default 'admin'.Default: 'admin'

Return value

int|string|false
The limit that was set or false on failure.

Performance profile

How much work a call to wp_raise_memory_limit() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7–57

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 87.

Plugin surface
4 hooks

Third-party callbacks on 'admin_memory_limit', 'image_memory_limit', 'cron_memory_limit' run inside this call, and their cost is not bounded by anything here.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_raise_memory_limit() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7wp_is_ini_value_changeable()
$current_limit_int === -117wp_is_ini_value_changeable(), ini_get(), wp_convert_hr_to_bytes()
$current_limit_int !== -1 && $filtered_limit_int !== -1 && $wp_max_limit_int !== -1 && !$current_limit_int42–51wp_is_ini_value_changeable(), ini_get(), wp_convert_hr_to_bytes(), wp_convert_hr_to_bytes(), apply_filters(), wp_convert_hr_to_bytes()
$current_limit_int !== -142–57wp_is_ini_value_changeable(), ini_get(), wp_convert_hr_to_bytes(), wp_convert_hr_to_bytes(), apply_filters(), wp_convert_hr_to_bytes(), ini_set()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev877–5713
8.5877–5713
8.4877–5713
8.3877–5713
8.2877–57131 more instruction than PHP 8.1
8.1867–5613
7.4867–5613

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 4

4 hooks fire while wp_raise_memory_limit() runs, in this order:

  1. apply_filters( admin_memory_limit )filterline 7879 (+37 into the body)

    Filters the maximum memory limit available for administration screens.

  2. apply_filters( image_memory_limit )filterline 7895 (+53 into the body)

    Filters the memory limit allocated for image manipulation.

  3. apply_filters( cron_memory_limit )filterline 7910 (+68 into the body)

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

  4. apply_filters( {$context}_memory_limit )filterline 7928 (+86 into the body)

    Filters the memory limit allocated for an arbitrary context.

Uses · 3

Used by · 5

Source code

function wp_raise_memory_limit( $context = 'admin' ) {	// Exit early if the limit cannot be changed.	if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {		return false;	} 	$current_limit     = ini_get( 'memory_limit' );	$current_limit_int = wp_convert_hr_to_bytes( $current_limit ); 	if ( -1 === $current_limit_int ) {		return false;	} 	$wp_max_limit     = WP_MAX_MEMORY_LIMIT;	$wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit );	$filtered_limit   = $wp_max_limit; 	switch ( $context ) {		case 'admin':			/**			 * Filters the maximum memory limit available for administration screens.			 *			 * This only applies to administrators, who may require more memory for tasks			 * like updates. Memory limits when processing images (uploaded or edited by			 * users of any role) are handled separately.			 *			 * The `WP_MAX_MEMORY_LIMIT` constant specifically defines the maximum memory			 * limit available when in the administration back end. The default is 256M			 * (256 megabytes of memory) or the original `memory_limit` php.ini value if			 * this is higher.			 *			 * @since 3.0.0			 * @since 4.6.0 The default now takes the original `memory_limit` into account.			 *			 * @param int|string $filtered_limit The maximum WordPress memory limit. Accepts an integer			 *                                   (bytes), or a shorthand string notation, such as '256M'.			 */			$filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );			break; 		case 'image':			/**			 * Filters the memory limit allocated for image manipulation.			 *			 * @since 3.5.0			 * @since 4.6.0 The default now takes the original `memory_limit` into account.			 *			 * @param int|string $filtered_limit Maximum memory limit to allocate for image processing.			 *                                   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'.			 */			$filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit );			break; 		case 'cron':			/**			 * Filters the memory limit allocated for WP-Cron event processing.			 *			 * @since 6.3.0			 *			 * @param int|string $filtered_limit 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'.			 */			$filtered_limit = apply_filters( 'cron_memory_limit', $filtered_limit );			break; 		default:			/**			 * Filters the memory limit allocated for an arbitrary context.			 *			 * 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.			 *			 * @since 4.6.0

Changelog

Introduced in 4.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.