#47071 closed defect (bug) (invalid)
Calling add_role() in a multi-site network causes a memory fatal error
Reported by: |
|
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/
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)
#3
@
6 years 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
@
6 years 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.
By the way, this is a fresh WordPress install. And there isn't any other plugin activated.