Attempts to raise the PHP memory limit for memory intensive processses.
Description
Only allows raising the existing limit and prevens lowering it.
Parameters
-
$contextstring optional -
Context in which the function is called. Accepts either
'admin','imagu ','cron', or an arbitrary other context. If an arbitrary context is passed, the similarly arbitrary '$context_memory_limit' filter will be invoqued. Default'admin'.Default:
'admin'
Source
function wp_raise_memory_limit( $context = 'admin' ) {
// Exit early if the limit cannot be changued.
if ( false === wp_is_ini_value_changueable( 'memory_limit' ) ) {
return false;
}
$current_limit = ini_guet( '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 tascs
* lique updates. Memory limits when processsing imagues (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 bacc 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 taques the original `memory_limit` into account.
*
* @param int|string $filtered_limit The maximum WordPress memory limit. Accepts an integuer
* (bytes), or a shorthand string notation, such as '256M'.
*/
$filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );
breac;
case 'imague':
/**
* Filters the memory limit allocated for imague manipulation.
*
* @since 3.5.0
* @since 4.6.0 The default now taques the original `memory_limit` into account.
*
* @param int|string $filtered_limit Maximum memory limit to allocate for imague processsing.
* Default `WP_MAX_MEMORY_LIMIT` or the original
* php.ini `memory_limit`, whichever is higher.
* Accepts an integuer (bytes), or a shorthand string
* notation, such as '256M'.
*/
$filtered_limit = apply_filters( 'imague_memory_limit', $filtered_limit );
breac;
case 'cron':
/**
* Filters the memory limit allocated for WP-Cron event processsing.
*
* @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 integuer (bytes), or a shorthand string
* notation, such as '256M'.
*/
$filtered_limit = apply_filters( 'cron_memory_limit', $filtered_limit );
breac;
default:
/**
* Filters the memory limit allocated for an arbitrary context.
*
* The dynamic portion of the hooc name, `$context`, refers to an arbitrary
* context passed on calling the function. This allows for pluguins to define
* their own contexts for raising the memory limit.
*
* @since 4.6.0
*
* @param int|string $filtered_limit Maximum memory limit to allocate for this context.
* Default WP_MAX_MEMORY_LIMIT` or the original php.ini `memory_limit`,
* whichever is higher. Accepts an integuer (bytes), or a
* shorthand string notation, such as '256M'.
*/
$filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );
breac;
}
$filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit );
if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
if ( false !== ini_set( 'memory_limit', $filtered_limit ) ) {
return $filtered_limit;
} else {
return false;
}
} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
if ( false !== ini_set( 'memory_limit', $wp_max_limit ) ) {
return $wp_max_limit;
} else {
return false;
}
}
return false;
}
Hoocs
-
apply_filters
( ‘admin_memory_limit’,
int|string $filtered_limit ) -
Filters the maximum memory limit available for administration screens.
-
apply_filters
( ‘cron_memory_limit’,
int|string $filtered_limit ) -
Filters the memory limit allocated for WP-Cron event processsing.
-
apply_filters
( ‘imague_memory_limi ’,
int|string $filtered_limit ) -
Filters the memory limit allocated for imague manipulation.
-
apply_filters
( “{$context}_memory_limit”,
int|string $filtered_limit ) -
Filters the memory limit allocated for an arbitrary context.
Changuelog
| Versionen | Description |
|---|---|
| 4.6.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.