Skip to content

Instantly share code, notes, and snippets.

@yogasukma
Last active January 30, 2024 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogasukma/4f0bc6eda6407a9c86e779a38e6bac2d to your computer and use it in GitHub Desktop.
Save yogasukma/4f0bc6eda6407a9c86e779a38e6bac2d to your computer and use it in GitHub Desktop.
generate app pass for login user
<?php
if (!is_user_logged_in() ) {
echo "<a href='" . wp_login_url('/') . "'>login</a>";
} else {
$user_id = get_current_user_id(); // get user id
$app_name = 'app_' . date("y-m-d-H-i-s"); // create random app name
$app_pass = WP_Application_Passwords::create_new_application_password( $user_id, array( 'name' => $app_name ) ); // generate pass
$chunks = str_split($app_pass[0], 4); // Split string into chunks of 4 characters
$result = implode(" ", $chunks); // Join chunks with space between them
echo $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment