Sandeep Kumar Mishra
Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.
This hook is handy for administrators who might need more memory for tasks such as updates. It’s important to note that memory limits for image processing (uploaded or edited by users of any role) are handled separately.
To use the admin_memory_limit filter, you first need to register it using add_filter. You can add this code to the functions.php file of your active theme or create a custom WordPress plugin.
At WePlugins, we always recommend creating a custom WordPress plugin when using hooks to avoid any issues when updating your WordPress theme in the future.
In the examples below, we define a function modify_admin_memory_limit_defaults which takes one parameter, and we register it using add_filter. The first parameter, admin_memory_limit, is the name of the hook. The second parameter, modify_admin_memory_limit_defaults, is the function to be called. The third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed to the registered function.
Sometimes, you may need to remove a registered hook, and you can use remove_filter to remove the admin_memory_limit filter.
Below is the parameter required to use this hook:
Below is an example of how you can use this hook:
function weplugins_modify_admin_memory_limit_defaults($filtered_limit) {
// Update the $filtered_limit variable according to your website requirements and return this variable. You can modify the $filtered_limit variable conditionally too if you want.
return $filtered_limit;
}
// add the filter
add_filter("admin_memory_limit", "weplugins_modify_admin_memory_limit_defaults", 10, 1);
Set a different memory limit based on certain conditions:
function weplugins_modify_admin_memory_limit_defaults($filtered_limit) {
if (is_admin()) {
$filtered_limit = '512M';
}
return $filtered_limit;
}
// add the filter
add_filter("admin_memory_limit", "weplugins_modify_admin_memory_limit_defaults", 10, 1);
To remove a hook callback, use the example below:
remove_filter("admin_memory_limit", "weplugins_modify_admin_memory_limit_defaults", 10, 1);
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
If you’re having any trouble using this hook or need customization, please contact us and we’d be happy to assist you.
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.