WordPress.org

Make WordPress Core

Opened 13 months ago

Closed 13 months ago

Last modified 13 months ago

#47071 closed defect (bug) (invalid)

Calling add_role() in a multi-site network causes a memory fatal error

Reported by: wahabmirjan Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.1.1
Component: Role/Capability Keywords:
Focuses: multisite Cc:

Description

Hello,

I am developing a plugin, in which I have to add a new role while activating it. Once I do that I get a fatal error. Here are the details.

<?php
function add_role () {
        
    // Create chart_user role if it does not exist
    $chart_user_role = get_role('no-access-role');
    if ($chart_user_role == null) {
        add_role('no-access-role', 'No-Access Role', array (
            'read' => false,
            'edit_posts' => false,
            'delete_posts' => false,
        ));
    }
}

register_activation_hook(__FILE__, 'add_role');

Once I activate the plugin, I get the following errors in the admin section

Plugin could not be activated because it triggered a fatal error.

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 262144 bytes) in C:\dev\wordpress\server\wp-content\plugins\test-plugin\backend\inc\activate.php on line 179

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0

I tried all possible solutions as suggested in many sites, including (and not limited to) the following:

https://www.wpbeginner.com/wp-tutorials/fix-wordpress-memory-exhausted-error-increase-php-memory/

https://stackoverflow.com/questions/21680244/fatal-error-allowed-memory-size-of-268435456-bytes-exhausted-tried-to-allocate

Once I remove the call to add the new role, things work OK. So the add_role() call is definitely the culprit.

Please note that I have a multi-site network setup using sub-domains.

Thanks.

Change History (7)

#1 @wahabmirjan
13 months ago

By the way, this is a fresh WordPress install. And there isn't any other plugin activated.

#2 @wahabmirjan
13 months ago

  • Severity changed from blocker to critical

#3 @thakkarhardik
13 months ago

Hi @wahabmirjan ,

It looks like that you are getting the fatal error because of the name of the callback function you have provided in register_activation_hook() i.e. 'add_role'. add_role() is a core wordpress function and you are using the same name for your callback function which causes the fatal error. Try changing the name of it and see if the issue still persists.

#4 @wahabmirjan
13 months ago

You are right. This was my bad in reporting the issue as well.

I omitted the namespace declaration (I did so on purpose to simplify the written code, but apparently that was not the right decision). The full code that generated the bug is as follows:

<?php
namespace the_awesome_code

function add_role () {
        
    // Create chart_user role if it does not exist
    $chart_user_role = get_role('no-access-role');
    if ($chart_user_role == null) {
        add_role('no-access-role', 'No-Access Role', array (
            'read' => false,
            'edit_posts' => false,
            'delete_posts' => false,
        ));
    }
}


register_activation_hook(__FILE__, 'the_awesome_code\add_role');

Note that the namespace keyword is added right now; however, running the above code will generate the error that was reported earlier.

That said, I decided to test the code as per your suggestion. So when changing the code where the add_role() function was renamed to add_my_awesome_role(), things worked correctly.

Obviously adding the namespace keyword is resulting in a loop to recursively call the add_role() function. I will update the code correctly.

#5 @wahabmirjan
13 months ago

  • Resolution set to invalid
  • Status changed from new to closed

#6 @SergeyBiryukov
13 months ago

  • Component changed from General to Role/Capability
  • Focuses multisite added
  • Severity changed from critical to normal

#7 @SergeyBiryukov
13 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.