0

I want to auto fill some details in contact 7 form , when user is logined and how to pass php value to js.

in function.php

if ( is_user_logged_in() ) {

 echo 'Welcome, registered user!';
     $current_user = wp_get_current_user();
    $custemail = $current_user->user_email;

function get_user_fild_details(){

?>

 <script type="text/javascript">
        function codeAddress() {
            var phoneMaddy = '<?php echo $custemail; ?>';
            alert('ok');
document.getElementById('nameMaddyC').value=phoneMaddy; 

        }

        window.onload = codeAddress;
        </script>

<?php
}    
add_action("wp_head", "get_user_fild_details");
 }

2 Answers 2

0

you can do with jquery like:

 <?php global $current_user;
  get_currentuserinfo();

  echo 'Username: ' . $current_user->user_login . "\n";
  echo 'User email: ' . $current_user->user_email . "\n";
  echo 'User level: ' . $current_user->user_level . "\n";
  echo 'User first name: ' . $current_user->user_firstname . "\n";
  echo 'User last name: ' . $current_user->user_lastname . "\n";
  echo 'User display name: ' . $current_user->display_name . "\n";
  echo 'User ID: ' . $current_user->ID . "\n";
  ?>

first get all login user data and then get contact form field id like #unsemae

$("#unsemae").val("<?php echo $current_user->user_login ;?>");

Thnk you

0
0

Finally i found the mistake i am doing.

    if ( is_user_logged_in() ) {


    function get_user_fild_details(){
**I just moved in to this function . I think its scope issue. Now its working fine** 
     echo 'Welcome, registered user!'; 
         $current_user = wp_get_current_user();
        $custemail = $current_user->user_email;
    ?>

     <script type="text/javascript">
            function codeAddress() {
                var phoneMaddy = '<?php echo $custemail; ?>';
                alert('ok');
    document.getElementById('nameMaddyC').value=phoneMaddy; 

            }

            window.onload = codeAddress;
            </script>

    <?php
    }    
    add_action("wp_head", "get_user_fild_details");
     }

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.