WordPress

Set WP_MEMORY_LIMIT value correctly in wp-config.php

Sharing is caring!

WordPress developers: please stay away from WP_MEMORY_LIMIT and PHP memory_limit settings! We see this done wrong over and over in WordPress plugins and themes. One of such themes is the premium theme Jupiter by Artbees, or WPML as plugin. WordPress users: don’t touch these memory limitation settings either! They’re imposed for a reason. Here’s some explanation:

In a situation where PHP memory_limit is set higher than WP_MEMORY_LIMIT this may cause problems.

Before we continue, it’s nice to know what WP_MEMORY_LIMIT is 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 PHP memory_limit is set to 134 MB.

This may interest you:   How to disable WordPress comments

Note: 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, and why?

The aforementioned 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 found 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' ).

This 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.

This may interest you:   A plea for WordPress plugin developers to stop supporting legacy PHP versions

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…

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;
	}

If you want to define WP_MEMORY_LIMIT, do it properly in your wp-config.php file

The fix for all this is quite easy. But only do this if plugins/themes won’t work properly otherwise!

Define WP_MEMORY_LIMIT in your wp-config.php file, and set it to your current PHP memory_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: 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.

If you are a WordPress plugin or theme developer: Please 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!


Leave a Reply

Your email address will not be published.


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.