wp_raise_memory_limit

Attempts to raise the PHP memory limit for memory intensive processes.

Description

(bool|int|string) wp_raise_memory_limit( (string) $context = 'admin' ); 

Only allows raising the existing limit and prevents lowering it.

Returns (bool|int|string)

The limit that was set or false on failure.

Parameters (1)

0. $context — Optional. (string) => 'admin'
The context.

Usage

  1. if ( !function_exists( 'wp_raise_memory_limit' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // The context. 
  5. $context = 'admin'; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_raise_memory_limit($context); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function wp_raise_memory_limit( $context = 'admin' ) { 
  2. // Exit early if the limit cannot be changed. 
  3. if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) { 
  4. return false; 
  5.  
  6. $current_limit = @ini_get( 'memory_limit' ); 
  7. $current_limit_int = wp_convert_hr_to_bytes( $current_limit ); 
  8.  
  9. if ( -1 === $current_limit_int ) { 
  10. return false; 
  11.  
  12. $wp_max_limit = WP_MAX_MEMORY_LIMIT
  13. $wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit ); 
  14. $filtered_limit = $wp_max_limit; 
  15.  
  16. switch ( $context ) { 
  17. case 'admin': 
  18. /** 
  19. * Filters the maximum memory limit available for administration screens. 
  20. * This only applies to administrators, who may require more memory for tasks 
  21. * like updates. Memory limits when processing images (uploaded or edited by 
  22. * users of any role) are handled separately. 
  23. * The `WP_MAX_MEMORY_LIMIT` constant specifically defines the maximum memory 
  24. * limit available when in the administration back end. The default is 256M 
  25. * (256 megabytes of memory) or the original `memory_limit` php.ini value if 
  26. * this is higher. 
  27. * @since 3.0.0 
  28. * @since 4.6.0 The default now takes the original `memory_limit` into account. 
  29. * @param int|string $filtered_limit The maximum WordPress memory limit. Accepts an integer 
  30. * (bytes), or a shorthand string notation, such as '256M'. 
  31. */ 
  32. $filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit ); 
  33. break; 
  34.  
  35. case 'image': 
  36. /** 
  37. * Filters the memory limit allocated for image manipulation. 
  38. * @since 3.5.0 
  39. * @since 4.6.0 The default now takes the original `memory_limit` into account. 
  40. * @param int|string $filtered_limit Maximum memory limit to allocate for images. 
  41. * Default `WP_MAX_MEMORY_LIMIT` or the original 
  42. * php.ini `memory_limit`, whichever is higher. 
  43. * Accepts an integer (bytes), or a shorthand string 
  44. * notation, such as '256M'. 
  45. */ 
  46. $filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit ); 
  47. break; 
  48.  
  49. default: 
  50. /** 
  51. * Filters the memory limit allocated for arbitrary contexts. 
  52. * The dynamic portion of the hook name, `$context`, refers to an arbitrary 
  53. * context passed on calling the function. This allows for plugins to define 
  54. * their own contexts for raising the memory limit. 
  55. * @since 4.6.0 
  56. * @param int|string $filtered_limit Maximum memory limit to allocate for images. 
  57. * Default '256M' or the original php.ini `memory_limit`,  
  58. * whichever is higher. Accepts an integer (bytes), or a 
  59. * shorthand string notation, such as '256M'. 
  60. */ 
  61. $filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit ); 
  62. break; 
  63.  
  64. $filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit ); 
  65.  
  66. if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) { 
  67. if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) { 
  68. return $filtered_limit; 
  69. } else { 
  70. return false; 
  71. } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) { 
  72. if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) { 
  73. return $wp_max_limit; 
  74. } else { 
  75. return false; 
  76.  
  77. return false;