Using WordPress ‘generate_recovery_mode_key’ PHP action

The generate_recovery_mode_key WordPress PHP action fires when a recovery mode key is generated.

Usage

add_action('generate_recovery_mode_key', 'your_custom_function', 10, 2);

function your_custom_function($token, $key) {
  // your custom code here
}

Parameters

  • $token (string) – The recovery data token.
  • $key (string) – The recovery mode key.

More information

See WordPress Developer Resources: generate_recovery_mode_key

Examples

Logging Recovery Mode Key Generation

Log each time a recovery mode key is generated.

add_action('generate_recovery_mode_key', 'log_recovery_key_generation', 10, 2);

function log_recovery_key_generation($token, $key) {
  // Log the recovery mode key generation
  error_log("Recovery mode key generated: {$key}");
}

Send Recovery Mode Key to Administrator

Send an email to the site administrator when a recovery mode key is generated.

add_action('generate_recovery_mode_key', 'send_recovery_key_email', 10, 2);

function send_recovery_key_email($token, $key) {
  $admin_email = get_option('admin_email');
  $subject = "Recovery mode key generated";
  $message = "A new recovery mode key has been generated: {$key}";

  // Send the email
  wp_mail($admin_email, $subject, $message);
}

Store Recovery Mode Key as Transient

Store the recovery mode key as a transient for 24 hours.

add_action('generate_recovery_mode_key', 'store_recovery_key_transient', 10, 2);

function store_recovery_key_transient($token, $key) {
  // Store the recovery key as a transient for 24 hours
  set_transient('recovery_key', $key, 24 * HOUR_IN_SECONDS);
}

Update Recovery Mode Key on External Service

Update the recovery mode key on an external service using an API.

add_action('generate_recovery_mode_key', 'update_recovery_key_on_service', 10, 2);

function update_recovery_key_on_service($token, $key) {
  // Update the recovery key on an external service
  $response = wp_remote_post('https://example.com/api/recovery-key', [
    'body' => ['key' => $key],
  ]);
}

Limit Recovery Mode Key Generation

Prevent recovery mode key generation if certain conditions are not met.

add_action('generate_recovery_mode_key', 'limit_recovery_key_generation', 10, 2);

function limit_recovery_key_generation($token, $key) {
  // Check if a user is logged in
  if (!is_user_logged_in()) {
    // Stop the recovery mode key generation
    wp_die("You must be logged in to generate a recovery mode key.");
  }
}