WP_MEMORY_LIMIT在wp-config中不生效,仅在default-constants.php生效问题
Let's break down how to fix this issue properly—because modifying core files like default-constants.php is never a sustainable solution (as you've noticed, WordPress updates will always overwrite those changes).
Why Your wp-config.php Setting Isn't Overriding the Default
Normally, any constant you define in wp-config.php should take priority over the defaults in default-constants.php. That core file uses defined() checks to only set the constant if it hasn't already been defined elsewhere. If your setting isn't sticking, here are the most likely culprits:
- A typo in your
define()statement (double-check the constant nameWP_MEMORY_LIMITand value format, e.g.,'256M'with proper quotes) - Another plugin or theme is resetting the memory limit after your
wp-config.phpdefinition - You accidentally placed the define statement in the wrong section of
wp-config.php
Step-by-Step Fixes
1. Correct Your wp-config.php Setup (No Core File Edits!)
First, delete any changes you made to default-constants.php—we're going to do this the right way.
Make sure your WP_MEMORY_LIMIT define is placed before the line that says require_once(ABSPATH . 'wp-settings.php'); (usually right before the closing ?> if it exists). Your code should look like this:
define( 'WP_DEBUG', true ); // Add your memory limit define right here define( 'WP_MEMORY_LIMIT', '256M' ); /* That's all, stop editing! Happy publishing. */ define( 'ABSPATH', dirname( __FILE__ ) . '/' ); require_once( ABSPATH . 'wp-settings.php' );
Double-check for typos—even a missing quote or lowercase letter can break this.
2. Force the Memory Limit via Code (If wp-config.php Still Fails)
If plugins or themes are overriding your setting, you can enforce it using a custom function. Add this to your active theme's functions.php file (or better yet, a must-use plugin so it doesn't get lost if you switch themes):
// Increase memory limit for admin area (adjust value as needed) function custom_increase_memory_limit() { if ( is_admin() ) { wp_raise_memory_limit( 'admin' ); // Or directly set it if wp_raise_memory_limit isn't enough: // ini_set( 'memory_limit', '256M' ); } } add_action( 'admin_init', 'custom_increase_memory_limit' );
Must-use plugins live in wp-content/mu-plugins/—create that folder if it doesn't exist, then add a file like increase-memory.php with the code above. This ensures the setting loads before any other plugins or themes.
3. Check for Conflicting Plugins/Themes
Temporarily disable all plugins and switch to a default theme (like Twenty Twenty-Four). If the memory limit starts working, re-enable plugins one by one to find which one is overriding your setting. Some optimization or security plugins might reset memory limits as part of their functionality.
Key Takeaway
Never edit WordPress core files—they'll be overwritten on every update. All customizations should happen in wp-config.php, your theme's functions.php, or custom/must-use plugins.
内容的提问来源于stack exchange,提问作者Papejoo

