Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use admin_memory_limit filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
October 10, 2022
5 minutes read

admin_memory_limit filter

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.

Parameters

    Below is the parameter required to use this hook:

  • $filtered_limit: (int|string) The maximum WordPress memory limit. Accepts an integer (bytes) or a shorthand string notation, such as ‘256M’.

Live Example

Example 1: Basic Usage

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

Example 2: Conditional Memory Limit

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

Example 3: Remove Hook Callback

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.

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook or need customization, please contact us and we’d be happy to assist you.

Sandeep Kumar Mishra

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.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.