0
votes

I try to implement a custom function/action after a new user profile has been created in WordPress. To test the code I try to write a text in a file as proof of function execution. Here is the code that I have inserted at the end of the active theme's functions.php file

if ( ! function_exists( 'register_for_cmsa' ) ) :
    function register_for_cmsa($user_id)    {
    // write in an external file
        // writeLogWP($msg) is a function from a file I have included in index.php
        writeLogWP("A new user is: " . $user_id);
    }
    add_action('user_register', 'register_for_cmsa');
endif;

Therefore, I added a new user through the admin panel. As soon as I validate the standard WP add user form (wp-admin/user-new.php) I get a blank page, meaning that the above code is in trouble. But the user is added in the database (it is visible in the users' list if I comment my function). The trouble here is when executing the writeLogWP("A new user is: " . $user_id) statement inside the register_for_cmsa() function. I tried to see if the statement works outside of the function, while always inside the functions.php file. And I noticed that it writes the message to the external file when I navigate in the WP site, BUT it publishes blank page as soon as I get into the admin dashboard section. My code is accepted in 'site' side but it is in error in the 'admin' one. The code is however not executed in the 'site' side because the hook is not triggered. It is triggered only if I go to the 'wp-admin/user-new.php' but ... I can' test it, as per the above reason. I am really confused, glad to have your comments and, why not, solution. Thanks

1
index.php is not accessed from the admin and you say you're including the function there. This might be the issue. What if you include it in functions.php ? What baffles me is you get blank screen instead of fatal error .. - Kaloyan
I learn things: index.php is not accessible in admin, although WP is the reference in MVC with frontend processor. Code completion in Eclipse mislead me. That may alsào explain why it is a code error - blank screen - when in dashboard mode (function not defined in fact). BB after test. - Ginger Opariti
@Kaloyan: Yes it works. Please copy your comment as an answer so that I can mark it so because you correctly pointed to the right direction: index.php is not reached from admin in WordPress! Thanks - Ginger Opariti
Sorry for the late response - I added it as an answer. Glad I could help. :) - Kaloyan

1 Answers

0
votes

The index.php is not loaded when accessing the WordPress admin, hence the writeLogWP function is not being called in that case. Moving it to functions.php should solve the issue.