Skip to content

Instantly share code, notes, and snippets.

@banagale
Last active March 18, 2021 18:01
Show Gist options
  • Save banagale/c3735ac99607ad2893b95132ab13f306 to your computer and use it in GitHub Desktop.
Save banagale/c3735ac99607ad2893b95132ab13f306 to your computer and use it in GitHub Desktop.
Output WordPress user authentication status to console
<?php
function display_wordpress_user_auth()
{ ?>
<script>
<?php
$current_user = wp_get_current_user();
if (isset($current_user)) {
if ($current_user->ID === 0) {
echo 'console.log(\'User is anonymous\');';
} else {
echo 'const wpUserEmail =\'' . $current_user->user_email . '\';' . PHP_EOL;
echo 'console.log(\'User is authenticated as: \', wpUserEmail);';
}
}
?>
</script>
<?php
}
?>
@banagale
Copy link
Author

Updated to PSR-12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment