wp_raise_memory_limit( string $context = 'admin' ): int|string|false
- Since
- 4.6.0
- Source
wp-includes/functions.php:7822
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
- Scaling
- Constant
- Instructions
- 7–57
- Plugin surface
- 4 hooks
- Called by
- 5
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 87.
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.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7 | wp_is_ini_value_changeable() |
$current_limit_int === -1 | 17 | wp_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_int | 42–51 | wp_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 !== -1 | 42–57 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 87 | 7–57 | 13 | |
| 8.5 | 87 | 7–57 | 13 | |
| 8.4 | 87 | 7–57 | 13 | |
| 8.3 | 87 | 7–57 | 13 | |
| 8.2 | 87 | 7–57 | 13 | 1 more instruction than PHP 8.1 |
| 8.1 | 86 | 7–56 | 13 | |
| 7.4 | 86 | 7–56 | 13 |
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:
- apply_filters( admin_memory_limit )filterline 7859 (+37 into the body)
Filters the maximum memory limit available for administration screens.
- apply_filters( image_memory_limit )filterline 7875 (+53 into the body)
Filters the memory limit allocated for image manipulation.
- apply_filters( cron_memory_limit )filterline 7890 (+68 into the body)
Filters the memory limit allocated for WP-Cron event processing.
- apply_filters( {$context}_memory_limit )filterline 7908 (+86 into the body)
Filters the memory limit allocated for an arbitrary context.
Uses · 3
- wp_is_ini_value_changeable()Determines whether a PHP ini value is changeable at runtime.
- wp_convert_hr_to_bytes()Converts a shorthand byte value to an integer byte value.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 5
- WP_Image_Editor_GD::load()Loads image from $this->file into new GD Resource.
- WP_Image_Editor_Imagick::load()Loads image from $this->file into new Imagick Object.
- stream_preview_image()Streams image in post to browser, along with enqueued changes in `$_REQUEST['history']`.
- unzip_file()Unzips a specified ZIP file to a location on the filesystem via the WordPress Filesystem Abstraction.
- wp_load_image()Load an image from a string, if PHP supports it.
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.0Changelog
Introduced in 4.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.