I'm looping through all users with
$allusers = get_users($args);
foreach ( $allusers as $user ):
And then I have an email user button (firstname and lastname are inserted into the subject), and a callback button (firstname and lastname are inserted into a hidden Contact Form 7 field)
The email link in my template is:
<a href="mailto:[email protected]?subject=<?php echo $user->first_name; ?> <?php echo $user->last_name; ?>">Email</a>
This works fine; it shows the correct user firstname and lastname in the loop. But the contact form field shows the last user in the loop, not the current one. This is what I have in my template:
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.callback').val('<?php echo $user->first_name; ?> <?php echo $user->last_name; ?>');
});
</script>
<?php echo do_shortcode( '[contact-form-7 id="12345"]' ); ?>
I have used this for inserting a variable into a Contact Form 7:
Why is my callback form variable displaying a different result to the email link variable, and how can I fix it?
I'm looping through all users with
$allusers = get_users($args);
foreach ( $allusers as $user ):
And then I have an email user button (firstname and lastname are inserted into the subject), and a callback button (firstname and lastname are inserted into a hidden Contact Form 7 field)
The email link in my template is:
<a href="mailto:[email protected]?subject=<?php echo $user->first_name; ?> <?php echo $user->last_name; ?>">Email</a>
This works fine; it shows the correct user firstname and lastname in the loop. But the contact form field shows the last user in the loop, not the current one. This is what I have in my template:
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.callback').val('<?php echo $user->first_name; ?> <?php echo $user->last_name; ?>');
});
</script>
<?php echo do_shortcode( '[contact-form-7 id="12345"]' ); ?>
I have used this for inserting a variable into a Contact Form 7: https://stackoverflow/questions/22943559/include-php-variable-in-contact-form-7-field
Why is my callback form variable displaying a different result to the email link variable, and how can I fix it?
I've only seen some parts of your code, but I guess I know where the problem lies...
If this is what you're printing in the loop:
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.callback').val('<?php echo $user->first_name; ?> <?php echo $user->last_name; ?>');
});
</script>
<?php echo do_shortcode( '[contact-form-7 id="12345"]' ); ?>
And you want to output multiple forms on the same page and every form should contain different user, then you can't do it like that. Why? Because of this part:
$('.callback')
Your forms and JS code is printed by PHP and everything works fine. But then, the JS is run ono the client-side in client browser, when the page is loaded. So the first JS code runs, selects all elements with class "callback" and sets its value. Then the second one runs and sets value of all "callback"s again... And so on...
One way to do this, the easiest fix, would be wrapping forms with div:
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.callback', '#user-form-<?php echo $user->ID; ?>').val('<?php echo $user->first_name; ?> <?php echo $user->last_name; ?>');
});
</script>
<div id="user-form-<?php echo $user->ID; ?>">
<?php echo do_shortcode( '[contact-form-7 id="12345"]' ); ?>
</div>
This way you can identify every form and set the fields inside that form only.
If get_users()
is returning results in a different order depending on when you call it, most likely something is changing the default order
and orderby
parameters.
I'd specifically declare them to ensure that you get the same order every time. In your $args
, add
$args = [
....
'orderby' => 'login',
'order' => 'ASC',
];