skip to Main Content

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?

2

Answers


  1. 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().

    Login or Signup to reply.
  2. 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( 'Harethinit', 'init' ) );
    
    function init() {
      $activation_token = wp_generate_password();
      ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search