I am using two WordPress plugins - Snippets for inserting PHP code and Scripts n Styles for inserting JavaScript.
My goal is to take a logged-in user's email address and pre-populate a form.
The PHP code in Snippets is:
<?php
function get_user() {
$current_user = wp_get_current_user();
$user_email = $current_user->user_email;
}
add_action( 'init', 'get_user', 10 );
?>
The code in Scripts n Styles is:
<script type="text/javascript">
window.onload = function () {
var useremail = "<?php echo json_encode($user_email); ?>";
document.getElementsByName("ElementName")[0].value = useremail;
}
</script>
But instead of getting [email protected] I get the quoted text inserted into the form:
<?php echo json_encode($user_email); ?>
Any idea of what is going wrong or a better way to achieve the same result?