I need to send a private e-mail address from a php file (page.php) to an Ajax Call in an other jquery file (sender.js). This e-mail address must be invisible to user/or website until the user get an automatic response from wp_mail, It must allowed it to answer to the organization.

This e-mail address is different according to the organization that the client want to contact (several hundred). I'm obliged to get the e-mail address from organization before form because it is generated by javascript. (generated_mail.js)

Sender.js is well called in header for page.php

I tried this code inside (page.php) - one organization is called with ajax inside page.php:

<?php $adressMailEntity = '[email protected] *'; ?> 
<script type="text/plain">var adressMailEntity = <?php echo $adressMailEntity ; ?> ;</script>
  • This e-mail address is for example , but must come from database. jquery code :
    var adressMailEntity;
                var error = false,
                    form = $(this).closest('form'),
                    busy = null;
                var formSerialize = form.serialize();
                var entityName = $('.s_ref_mail').text();
                var entityLogo = $('.atv_bg_WinEnterprise .logo').attr('src');
                //How to get php var $adressMailEntity , or jquery var adressMailEntity ?
                    if (busy)
                    busy = $.ajax({
                        url : ajaxSender.ajax_url,
                        data : {
                            action : 'mail_form',
                            nonce: ajaxSender.nonce,
                            formFields : formSerialize,
                            entityName : entityName,
                            entityLogo : entityLogo,
                            adressMailEntity : adressMailEntity
                        type : 'POST',
                        success : function(response){
                        error : function(response){  
                return false;
  • So the issue is adressMailEntity does not contain the value you expect it to (the email you fetched in PHP when generating the page?) Or does adressMailEntity does not exist? Or is there an error message in the console that happens when the AJAX call occurs? Or is it that the AJAX handler in PHP does not receive the adressMailEntity value when your JS makes the request? Use the edit link under the tags to update your Q to provide as specific detail for why it fails, how you know that it failed, and where it fails. As well as if it doesn't do what you expect, what does it do instead?
    – Tom J Nowell
    Commented Nov 26, 2021 at 18:15
  • @Tom J Nowell I got errors: either I get only variable &adressMailEntity or I did Null or undefied. the solution of Pixelsmith is good, but I'm trying to hide the email address between the php variable (data-adressMailEntity) and the retrieval of the value with jquery. Is ti possible ?
    – imagIne
    Commented Nov 27, 2021 at 12:36
  • that data must be passed, there is no way to pass it without passing it, there is no way to get the email to javascript without putting it in the page or retrieving it from the server. As for why your code fails, this looks like it's a generic JS problem that has nothing to do with WordPress, specifically, the order things appear on the page, and scope. The adressMailEntity your jQuery code is using and the adressMailEntity your PHP writes out to the document are not the same variable. They have the same name yes! But the same scope no. window.adressMailEntity would be better.
    – Tom J Nowell
    Commented Nov 27, 2021 at 20:33
  • If you can confirm the PHP is inserting the value as expected, then you should be able to ask on stackoverflow and anybody with HTML/JS knowledge can help, WordPress knowledge isn't necessary. I will say though, that your PHP code fails to wrap your value in quotes, which is a problem, that would have shown up in the browsers dev console too.
    – Tom J Nowell
    Commented Nov 27, 2021 at 20:34

1 Answer 1


I use the data attribute to pass variables from PHP to JS.

<div data-your-php-variable="<?php echo $yourCoolVar; ?>"></div> 
  • 1
    Your solution is good, but I would to hide the e-mail address between php and jquery file .Is it possible ? Php : <?php $adressMailEntity = '[email protected]'; ?> <div id="am_entity" style="display:none;" data-amEntity="<?php echo $adressMailEntity; ?>"> </div> Jquery : var adressMailEntity = $('#am_entity').attr('data-amEntity'); This code work.
    – imagIne
    Commented Nov 27, 2021 at 12:39
  • That would appear to do the trick!
    – Pixelsmith
    Commented Nov 27, 2021 at 19:43
  • I would not display anywhere the e-mail address . It must not display inside html code . I read about base64_encode but I’m not sure it’s the good idea ! there are many sites that allow you to decode base64! I can’t retreive php var without using echo inside Ajax call.
    – imagIne
    Commented Nov 27, 2021 at 22:53
  • I see what you're saying now. There are ways around this. So, assume the address is '[email protected]'. You can remove the @ and . and recombine them in the JS. Something like data-name="imagine" data-url="gmail", then in the JS you can concatenate the strings to form the original email address.
    – Pixelsmith
    Commented Nov 28, 2021 at 23:19
  • 1
    Ok, thanks. I will see how to do otherwise
    – imagIne
    Commented Dec 1, 2021 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.