The WordPress memory limit can be increased by the WP_MEMORY_LIMIT variable in wp-config.php
. However, I see this done wrong over and over again in WordPress plugins and themes. In a worst case scenario this may even decrease the available amount of memory for WordPress! So be careful with the advice you follow. In a situation where PHP memory_limit is set higher than a custom WP_MEMORY_LIMIT in wp-config.php, this may cause problems.
Update 2022-08: Parts of this post are obsolete and caught up by time. This post will be rewritten soon. See https://core.trac.wordpress.org/ticket/56390.
Read on, and I'll try to explain it all.
What is WordPress WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT
Before we continue, it's nice to know what WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT are used for. WordPress writes on the Codex:
Also released with Version 2.5, the WP_MEMORY_LIMIT option allows you to specify the maximum amount of memory that can be consumed by PHP. This setting may be necessary in the event you receive a message such as "Allowed memory size of xxxxxx bytes exhausted".
This setting increases PHP Memory only for WordPress, not other applications. By default, WordPress will attempt to increase memory allocated to PHP to 40MB (code is at the beginning of /wp-includes/default-constants.php) for single site and 64MB for multisite, so the setting in wp-config.php should reflect something higher than 40MB or 64MB depending on your setup.
WordPress will automatically check if PHP has been allocated less memory than the entered value before utilizing this function. For example, if PHP has been allocated 64MB, there is no need to set this value to 64M as WordPress will automatically use all 64MB if need be.
Please note, this setting may not work if your host does not allow for increasing the PHP memory limit--in that event, contact your host to increase the PHP memory limit. Also, note that many hosts set the PHP limit at 8MB.WordPress Codex - Increasing memory allocated to PHP
Now imagine your web server's PHP memory_limit
is set to 134 MB.
How WordPress defines memory limits
WordPress defines and sets memory limits in the file ./wp-includes/default-constants.php
, on lines 32 - 44 (source: https://github.com/WordPress/WordPress/blob/master/wp-includes/default-constants.php):
$current_limit = @ini_get( 'memory_limit' );
$current_limit_int = wp_convert_hr_to_bytes( $current_limit );
// Define memory limits.
if ( ! defined( 'WP_MEMORY_LIMIT' ) ) {
if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
define( 'WP_MEMORY_LIMIT', $current_limit );
} elseif ( is_multisite() ) {
define( 'WP_MEMORY_LIMIT', '64M' );
} else {
define( 'WP_MEMORY_LIMIT', '40M' );
}
}
The PHP resource limit memory_limit is changeable in PHP_INI_ALL, meaning it can be set (changed) anywhere - normally. This should return true
for wp_is_ini_value_changeable( 'memory_limit' )
.
Because if this, it defines WP_MEMORY_LIMIT
as 40M as lower limit - for a single site installation, when memory_limit
may be changed. In short, the difference between WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT is:
This setting [WP_MEMORY_LIMIT
] increases PHP Memory only for WordPress, not other applications. By default, WordPress will attempt to increase memory allocated to PHP to 40MB (code is at the beginning of /wp-includes/default-constants.php) for single site and 64MB for multisite, so the setting in wp-config.php should reflect something higher than 40MB or 64MB depending on your setup.
Administration tasks require much memory than usual operation. When in the administration area, the memory can be increased or decreased from the WP_MEMORY_LIMIT by defining WP_MAX_MEMORY_LIMIT.
So WP_MEMORY_LIMIT
is the limit and WP_MAX_MEMORY_LIMIT
, if set, will override the former in the wp-admin Dashboard.
Do you you want to increase WP_MEMORY_LIMIT? Do it properly in your wp-config.php file
So you want to increase the WordPress memory limit? Do it in your wp-config.php file as follows. The fix for all this is quite easy, but only do this if plugins and themes don't work properly without changing settings.
Set a PHP memory limit in a user-defined .user.ini file:
In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in
$_SERVER['DOCUMENT_ROOT']
). In case the PHP file is outside the document root, only its directory is scanned.Only INI settings with the modes PHP_INI_PERDIR and PHP_INI_USER will be recognized in .user.ini-style INI files.
PHP Manual .user.ini files
memory_limit = 256M
After increasing PHP's memory limit, you can now create a WP_MEMORY_LIMIT constant in your wp-config file. Use the following syntax to set it to your currently configured PHP limit.
define( 'WP_MEMORY_LIMIT', ini_get( 'memory_limit' ) );
Add the above line just before the line "/* That's all, stop editing! Happy blogging. */".
Setting this constant may throw a PHP Notice when put below this line. The PHP Notice is for example (on one line): PHP Notice: Constant WP_MEMORY_LIMIT already defined in /path/to/example.com/www/wp-config.php on line 96
As always, it's best to trust your hosting provider and to not fiddle with memory settings. WordPress loving hosting providers will set reasonable limitations, as you may expect. So your WordPress website always runs as smooth as possible. WordPress plugin and theme developers: Stay away from fiddling with PHP/WordPress memory limits! If you are a WordPress user: don't fiddle with PHP/WordPress memory settings either!
In this post you learned a correct way of setting WordPress WP_MEMORY_LIMIT and PHP memory_limit settings.
Questions? Remarks? Leave them as a comment please, thanks!