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 this post I show you a correct way of setting WordPress WP_MEMORY_LIMIT and PHP memory_limit settings.

How to increase the memory limit for your WordPress website, the right way. To increase WordPress speed & performance.

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.

The reason for this blog post is: in a situation where PHP memory_limit is set higher than a custom WP_MEMORY_LIMIT in wp-config.php, this may causes problems. You are basically decreasing the amount of memory available to WordPress through WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT. So something you don't often have to do is: define( 'wp_memory_limit', '256M' ).

WordPress theme & plugin developers: please stay away from WP_MEMORY_LIMIT and PHP memory_limit settings! This is done wrong over and over in plugins and themes. WordPress users: don't fiddle with these memory limitation settings either. They're imposed for a reason. Let WordPress Core handle this.

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.

the mentioned themes and plugins are just examples, this is not a rant towards them. Have you ever needed to set a different memory limit? Please let me know why in the comments, thanks!

The Jupiter theme has the following PHP code snippet in wp-content/themes/jupiter/framework/admin/control-panel/logic/compatibility.php:

public function phpIniCheck()
{
	// ...
	$incorrect_memory_limit        = ($this->let_to_num(WP_MEMORY_LIMIT) < 100663296);
	// ...
	if ($incorrect_memory_limit)
	{
		$response[] = [
			'sys_msg'       => __('WordPress Memory Limit', 'mk_framework') . ': ' . WP_MEMORY_LIMIT . ', ',
			'sys_recommend' => __('memory_limit should be at least 96MB.', 'mk_framework'),
			'link_href'     => 'http://codex.wordpress.org/Editing_wp-config.php#Increasing_memory_allocated_to_PHP',
			'link_title'    => __('Increasing memory allocated to PHP', 'mk_framework'),
			'type'          => 'error',
			'status'        => false,
		];
	}
	// ...

The function let_to_num() is located in wp-content/themes/jupiter/framework/admin/control-panel/logic/functions.php.

This function phpIniCheck() checks whether WP_MEMORY_LIMIT is defined and set high enough: more than 96 MB. If that's not the case, show an error. It's the check for WP_MEMORY_LIMIT that's used wrong here.

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. And back to the Jupiter theme. Take a look at the phpIniCheck() function. Here's the logic of what it does:

  1. is WP_MEMORY_LIMIT defined and smaller than 100663296 bytes (96 MB)?
  2. throw an error

We've just learned that normally WP_MEMORY_LIMIT is defined to a lower limit of 40M, because memory_limit may be changed anywhere by PHP, and then wp_is_ini_value_changeable returns true.

The check Artbees/Jupiter uses checks if the 40M WP_MEMORY_LIMIT is lower than 96M (100663296 bytes). The check evaluates as $true, setting the variable $incorrect_memory_limit to true, and the error message is displayed.

Pff... Someone really needs to look at this WP_MEMORY_LIMIT logic in WordPress... And WP_MAX_MEMORY_LIMIT...

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.

WPML?

You mentioned WPML? Yes I did.

In sitepress-multilingual-cms/classes/troubleshoot/class-wpml-debug-information.php WPML inaccurately assumes WP_MEMORY_LIMIT is the current imposed memory limit:

function get_core_info() {

	$core = array(
		// ...
		'PHP'       => array(
			'MemoryLimit'     => ini_get( 'memory_limit' ),
			'WP Memory Limit' => WP_MEMORY_LIMIT,
			'UploadMax'       => ini_get( 'upload_max_filesize' ),
			'PostMax'         => ini_get( 'post_max_size' ),
			'TimeLimit'       => ini_get( 'max_execution_time' ),
			'MaxInputVars'    => ini_get( 'max_input_vars' ),
			'MBString'        => $this->sitepress->get_wp_api()->extension_loaded( 'mbstring' ),
			'libxml'          => $this->sitepress->get_wp_api()->extension_loaded( 'libxml' ),
		),
	);

	return $core;
}

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 - like Vevida.com - 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!

Questions? Remarks? Leave them as a comment please, thanks!

Donate a cup of coffee
Donate a cup of coffee

Thank you very much! <3 ❤️