0

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:info@example.co.uk?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.com/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?

  • Please post the entire loop so we can see where things are happening. Right now it is too difficult to piece together. – Fencer04 Nov 20 '18 at 20:07
1

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...

So how to deal with that?

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.

0

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',
];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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