phpwordpresswordpress-plugin-creationultimate-member

username_exists function in wordpress throwing 500 error (function not defined), despite require/include pluggable.php and user.php


In my custom plugin's php, trying to call the core wordpress function username_exists() throws a 500 error which I think is caused by that function not being defined.

The line in my code that is failing is:

$unres = username_exists($unt);

I have verified that it is not a problem caused by a null argument, as the following works as expected: $unres = $unt;

How do I fix this?

I tried all the solutions throughout the answers and comments at all of the following:

'username_exists' function not working in Wordpress plugin

Function username_exists() can't be accessed without logging in to wordpress

WordPress plugin admin page - using WordPress function in linked php file

https://wordpress.stackexchange.com/questions/20915/wordpress-plugin-admin-page-using-wordpress-function-in-linked-php-file

How do I call the basic WordPress header files?

wordpress plugin -> Call to undefined function wp_get_current_user()

I have successfully added the following php to include/require these files (but this did not help):

require realpath("../../../../../wp-includes/pluggable.php");
require realpath("../../../../../wp-includes/user.php");
require realpath("../../../../../wp-admin/includes/user.php");

If I include or require the following, it creates a fatal site error:

require realpath("../../../../../wp-load.php");

Is there now some other core file that I need to 'include' in my php by reference, other than those outlined above (for example, due to new WP versions since those questions were written)? (I'm on WP v5.5).

Do I need to contextualise the call? (e.g. reference namespace, call as public/global, etc?)

(Note: the site also uses the Ultimate Member plugin, not sure if that would override the core function name in any way?)

Thanks.


Solution

  • Instead of running raw PHP code, it is really a best practice to run your code in the context of WordPress. WordPress has many APIs available and for a JavaScript-based call the REST is probably the best choice.

    Below is really simple code that registers a REST route and tests the supplied parameter against the core username_exists function. I've included inline comments which should explain everything, but once you remove those and collapse some whitespace, you'll see it is only 20 lines or so of code.

    // Always register routes in this action
    add_action(
        'rest_api_init',
    
        // My IDE is happiest when I make my functions static, but that is not a requirement
        static function () {
    
            register_rest_route(
    
            // The shared namespace for all route in this plugin
                'ed2/v1',
    
                // The path with optional parameters for this specific route
                '/username/(?P<username>.*?)',
                [
                    // We only accept GET, but could be POST or an array of methods
                    'methods' => 'GET',
    
                    // The function to run
                    'callback' => static function (WP_REST_Request $request) {
    
                        // Our parameter was named above so we can fetch it here 
                        $username = $request->get_param('username');
    
                        // Return an array, the API will handle turning it into a JSON response
                        return [
                            'username' => $username,
                            'exists' => username_exists($username),
                        ];
                    },
                ]
            );
        }
    );
    

    In your browser, you should now be able to go to http://example.com/wp-json/ed2/v1/username/test and it should return (assuming you don't have a username of test):

    {"username":"test","exists":false}
    

    If you change to a username that does exist, such as https://example.com/wp-json/ed2/v1/username/user@example.com you should get:

    {"username":"user@example.com","exists":1}
    

    To restate what I said at the beginning, I differentiate between PHP code calling WordPress functions which generally requires "waking WordPress up", and WordPress code calling PHP functions which means that WordPress is "already awake". You can run PHP code that boots WordPress by including wp-load.php, but you also shouldn't be surprised if certain things don't work. What those things are that could break really depends on your environment.