Home > OS >  Fatal error: Uncaught Error: Call to undefined function wp_generate_password() or wp_mail()
Fatal error: Uncaught Error: Call to undefined function wp_generate_password() or wp_mail()

Time:11-05

I would like to generate a unique password hash and try to use the following wordpress function:

$activation_token = wp_generate_password();
update_user_meta($user_id, 'activation_token', $activation_token);

Unfortunately, it not work because Fatal error: Uncaught Error: Call to undefined function wp_generate_password()

Is this function deprecated? I cannot find anything else in WordPress documentation.

Edit: I try the code in my wordpress theme and in works! So the problem is, i cannot call any wordpress core functions? I have the same problem with wp_mail()

I updated the wordpress installation, use the default Twenty theme and deactivated all other plugins but i have still the same probelm! wp_ functions not work in plugin, why?

CodePudding user response:

You can't call many parts of the WordPress system until it's loaded. Most stuff in plugins needs to be invoked from within the init action handler, or some other filter or action handler.

Try something like this.

<?php
namespace Hareth;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
  exit;
}

add_action( 'init', array( 'Hareth\init', 'init' ) );

function init() {
  $activation_token = wp_generate_password();
  ...
}

CodePudding user response:

Check this out https://developer.wordpress.org/reference/functions/wp_generate_password/

Uses wp_rand() to create passwords with far less predictability than similar native PHP functions like rand() or mt_rand().

  • Related