1

I'm developing a WordPress website that allows input via custom fields and then outputs that onto a different page. When a user signs up, they ask someone to be a "trusted member" by sending them an invite link which has a unique identifier which when the invitee registers, the UID is stored as metadata on their account.

This metadata is then used (or supposed to be) to create a section on a page that displays some of the information about the person who invited them.

(Long story short, the site is a memorial type site that someone uploads their last wishes to. The trusted verifier is a nominated person who informs the site owner of their members death so the site owner can act on their wishes.)

Basically, what I'm trying to do is grab the metadata from the user account then use that to grab the name of the person.

I've added the full code for this function into codepen here: https://codepen.io/david-wdc/pen/LYEoOXe

add_shortcode('trusted_verifier_dashboard', 'trusted_verifier_dashboard');

function trusted_verifier_dashboard(){

  ob_start();
  $user_id = get_current_user_id();

  $current_user = wp_get_current_user();

  if ( in_array( 'trusted', (array) $current_user->roles ) ) {
    //The user has the "trusted" role

    $unique_list = get_user_meta( $user_id, 'tv_member' );
    $id_list = get_user_meta( $user_id, 'tv_uid' );
    $row_list = get_user_meta( $user_id, 'tv_row' );

    var_dump($unique_list);
    var_dump($id_list);
    var_dump($row_list);

     foreach($unique_list as $row => $value){
         echo ($value);
     }


       /*
       *         tv_member  {
       *                array (0 => '9dz4KEPQHB6ZCKjZ9MG2Jl');
       *         }
       *         tv_row  {
       *                array (0 => '1');
       *         }
       *         tv_uid  {
       *                array (0 => '1');
       *         }
       */

foreach($id_list as $row => $array)
{

  $member_id = $tv_uid;
  $unique = $tv_member;
  $row = $tv_row;
  $image = '';

  $unique_lookup = get_user_meta($member_id, 'trusted_verifiers_'.$row_list.'_tv_unique', true);

  $fname = get_user_meta($member_id, 'first_name', true);
  $sname = get_user_meta($member_id, 'last_name', true);
  $city = get_user_meta($member_id, 'billing_city', true);
  $photo = get_user_meta($member_id, '_memorial_photo_0_upload_image', true);

  if ($unique == $unique_lookup){
       echo "Unique code matches (MU:".$unique_lookup." / U:".$unique.")<br/>";

      ?>
                                                            <div id="trusted-row-<?php echo get_row_index(); ?>" class="vt-wrap">
                                                                <div class="vt-status">
                                                                    <?php

     if( have_rows('memorial_photo', 'user_'.$member_id) ): 
      while( have_rows('memorial_photo', 'user_'.$member_id) ): the_row();

        // vars
        $field = get_sub_field('upload_image');
        $image = $field['url'];

        if( $image ) {
        echo '<img class="tv-avatar" src="'.$image.'"/>';
        }

      endwhile; 

    endif;

     ?> </div>
    <div class="vt-left">
       <h3 style="margin-top:15px;">Member Details</h3>
        <p class="vt-name">
            Name: <?php echo $fname." ".$sname; ?>
        </p>
        <p class="vt-relation">
            Location: <?php echo $city; ?>
        </p>
    </div>
    <div class="vt-center">
        <p class="vt-added">

        </p>
        <hr>
        <p class="vt-request-added">

        </p>
    </div>
    <div class="vt-right">
        <div class="vt-button"> <a id="vt-<?php echo $member_id ?>" class="inform-button button-primary" href="#popup<?php echo $member_id ?>">Inform mySwanSong</a>
            <div id="success"> </div>
            <div id="popup<?php echo $member_id ?>" class="popoverlay">
                <div class="popup">
                    <h3>Are you sure you wish to proceed?</h3> <a class="close" href="#">&times;</a>
                    <div class="content">
                        <div class="pu-member">
                            <?php
if( have_rows('memorial_photo', 'user_'.$member_id) ): 
while( have_rows('memorial_photo', 'user_'.$member_id) ): the_row();

// vars
$field = get_sub_field('upload_image');
$image = $field['url'];

if( $image ) {
echo '<img class="tv-avatar" src="'.$image.'" width="75px" height="75px;" style="border-radius:5px;"/>';
}

endwhile; 

endif;
?>
        <p class="pu-name">
            <?php echo $fname." ".$sname; ?>
        </p>
</div>
<div class="message">

        <p>Please enter their date of death:</p>
        <input id="date<?php echo $member_id ?>" name="date" />
        <hr /> <small>By clicking 'YES' below, you will notify mySwanSong about the passing of this member.</small> </div>
<div class="pu-buttons"> <a class="useful" href="/useful-links/" style="display:none;">Useful Links</a>
    <input id='ajaxButton' type="submit" onclick="imAnAjaxFunction(jQuery(this));" />
    <?php

// $m_id 
// $unique
// $row
// $unique_lookup 
// $fname 
// $sname 
// $city
// $photo


$c_email = esc_html( $current_user->user_email );
$c_fname = esc_html( $current_user->user_firstname );
$c_sname = esc_html( $current_user->user_lastname );



?>
<script>
    function imAnAjaxFunction($this) { //If you got error "Function ajax is not defined" declare this before document ready
        var member_name = "<?php echo $fname." ". " ".$sname; ?>";
        var member_id = "<?php echo $member_id; ?>";
        var current_name = "<?php echo $c_fname." ". " ".$c_sname; ?>";
        var current_email = "<?php echo $c_email; ?>";
        var current_id = "<?php echo $user_id; ?>";
        var get_date = jQuery('#date<?php echo $m_id ?>').val();
        jQuery.ajax({
            method: "POST"
            , context: this
            , async: false
            , url: "<?php echo esc_js( admin_url( 'admin-ajax.php' ) ) ?>"
            , dataType: "text"
            , data: {
                action: "email_mss_admins", // call the php function 
                member_name: member_name
                , member_id: member_id
                , current_name: current_name
                , current_email: current_email
                , current_id: current_id
                , get_date: get_date
            }
            , success: function (response) {
                console.log(get_date);
                jQuery('#popup<?php echo $member_id ?> h3').fadeOut('slow', function () {
                    jQuery('#popup<?php echo $member_id ?> h3').html("Thank You");
                    jQuery('#popup<?php echo $member_id ?> h3').fadeIn('slow');
                });
                jQuery('#popup<?php echo $member_id ?> .pu-member').fadeOut('slow');
                jQuery('#popup<?php echo $member_id ?> .message').fadeOut('slow', function () {
                    jQuery('#popup<?php echo $member_id ?> .message').html("my SwanSong will contact you in due course if we need to confirm any information.  On behalf of my SwanSong, we are sincerely sorry for your loss and during this difficult time, we have provided some links to some useful organisations that may provide support and comfort for you, your family and friends’.");
                    jQuery('#popup<?php echo $member_id ?> .message').fadeIn('slow');
                    jQuery('#popup<?php echo $member_id ?> #ajaxButton').fadeOut('slow', function () {
                        jQuery('#popup<?php echo $member_id ?> #ajaxButton').html("Thank You");
                        jQuery('#popup<?php echo $member_id ?> .useful').fadeIn('slow');
                    });
                });
            }
        });
    }
</script>
<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery('#date<?php echo $member_id; ?>').datepicker({
            dateFormat: 'dd/mm/yy'
            , maxDate: new Date
        });
    });
</script>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php

                    } else {
                       echo "Unique code mismatch (MU:".$unique_lookup." / U:".$unique."<br/>";
                  }

                  ?>
                                                                                <?php 
              }



    }

    return ob_get_clean();

    }`

(I've added a comment with some of the metadata that it's pulling)

If you need me to add more information/code or you've got a question about it, just ask!

Thanks

10
  • 2
    Just as a general tip, this isn't code review, but cut up your hugemongous function in several functions. This is too long, has too many responsibilities and is generally unreadable. What happens with your code if someone enters a " in a field like $fname or $c_email or $c_sname? It will break your code massively. Make sure all your output is sanitized and conform acceptable output. also, you'd better put those variable values in data-attribute-name="" on the <input id="ajaxbutton" data-c_email="<?= htmlspecialchars($c_email) ?>"> and then just do var c_data = $this.data('c_email') Commented Jan 29, 2020 at 15:06
  • 1
    Hey man, thanks for the response @Tschallacka. I will certainly see if I can refine some of the code. I know it will run better once done. That's not my main issue though, the meta data isn't being pulled onto the page, the ajax function is to send an email to the site owner, I need the PHP above to pull the metadata onto the page. My head hurts haha. Any light in terms of that respect? Commented Jan 29, 2020 at 16:49
  • 1
    follow the data. Dump it along the way from $current_user to $tv_uid to $member_id to get_user_meta and lay that along the values in the database. Commented Jan 29, 2020 at 17:34
  • 1
    There is not enough "Salvageable" to make it a proper answer that will help other future visitors, and I don't really need the rep, got enough as it is. Without having the metadata setup, data present, basically access to your environment, it will be a lot harder to give a proper answer. You can self answer when you've found the solution. I'll upvote you then. Commented Jan 29, 2020 at 19:29
  • 1
    @Tschallacka fair play man, appreciate that. Commented Jan 29, 2020 at 19:37

1 Answer 1

1

I have managed to fix this,

I was trying to pass through a whole object into a function but it was only expecting a single element in the object.

The new code is below if anybody comes across something similar!

add_shortcode('trusted_verifier_dashboard', 'trusted_verifier_dashboard');

function trusted_verifier_dashboard(){

ob_start();
$user_id = get_current_user_id();

$current_user = wp_get_current_user();

if ( in_array( 'trusted', (array) $current_user->roles ) ) {
//The user has the "trusted" role

//$unique_list = get_user_meta( $user_id, 'tv_member' );
$id_list = get_user_meta( $user_id, 'tv_uid' );
//$row_list = get_user_meta( $user_id, 'tv_row' );

//var_dump($unique_list);
//var_dump($id_list);
//var_dump($row_list);

 //foreach($unique_list as $row => $value){
     //echo ($value);
 //}

 $current_user_id = get_current_user_id();
 //echo "Current User Id: "; print_r ($current_user_id);



 foreach($id_list as $row => $array) {

          $member_id = $array;
          $unique = get_user_meta($current_user_id, 'tv_member', true);
          //$row = get_user_meta($current_user_id, 'tv_row', true);
          $image = '';
          $unique_lookup = get_user_meta($member_id, 'trusted_verifiers_'.$row.'_tv_unique', true);
          $fname = get_user_meta($member_id, 'first_name', true);
          $sname = get_user_meta($member_id, 'last_name', true);
          $city = get_user_meta($member_id, 'billing_city', true);
          $photo = get_user_meta($member_id, '_memorial_photo_0_upload_image', true);

          /*echo "<br>member_id: "; print_r ($array); echo "<br>unique key: ";print_r ($unique); echo "<br>unique lookup: "; print_r ($unique_lookup); echo "<br>row: "; print_r ($row);*/

          if ($unique == $unique_lookup){
              // echo "<br>Unique code matches (MU:".$unique_lookup." / U:".$unique.")<br/>";

              ?>
<div id="trusted-row-<?php echo get_row_index(); ?>" class="vt-wrap">
<div class="vt-status">
    <?php

             if( have_rows('memorial_photo', 'user_'.$member_id) ): 
              while( have_rows('memorial_photo', 'user_'.$member_id) ): the_row();

                // vars
                $field = get_sub_field('upload_image');
                $image = $field['url'];

                if( $image ) {
                echo '<img class="tv-avatar" src="'.$image.'"/>';
                }

              endwhile; 

            endif;

             ?> </div>
<div class="vt-left">
    <h3 style="margin-top:15px;">Member Details</h3>
    <p class="vt-name">
        Name: <?php echo $fname." ".$sname; ?>

    </p>
    <p class="vt-relation">
        Location: <?php echo $city; ?>
    </p>
</div>
<div class="vt-center">
    <p class="vt-added">

    </p>
    <hr>
    <p class="vt-request-added">

    </p>
</div>
<div class="vt-right">
    <div class="vt-button"> <a id="vt-<?php echo $member_id ?>" class="inform-button button-primary" href="#popup<?php echo $member_id ?>">Inform mySwanSong</a>
        <div id="success"> </div>
        <div id="popup<?php echo $member_id ?>" class="popoverlay">
            <div class="popup">
                <h3>Are you sure you wish to proceed?</h3> <a class="close" href="#">&times;</a>
                <div class="content">
                    <div class="pu-member">
                        <?php
                                 if( have_rows('memorial_photo', 'user_'.$member_id) ): 
                        while( have_rows('memorial_photo', 'user_'.$member_id) ): the_row();

                          // vars
                          $field = get_sub_field('upload_image');
                          $image = $field['url'];

                          if( $image ) {
                          echo '<img class="tv-avatar" src="'.$image.'" width="75px" height="75px;" style="border-radius:5px;"/>';
                          }

                        endwhile; 

                      endif;
                        ?>
                        <p class="pu-name">
                            <?php echo $fname." ".$sname; ?>
                        </p>
                    </div>
                    <div class="message">

                        <p>Please enter their date of death:</p>
                        <input id="date<?php echo $member_id ?>" name="date" />
                        <hr /> <small>By clicking 'YES' below, you will notify mySwanSong about the passing of this member.</small>
                    </div>
                    <div class="pu-buttons"> <a class="useful" href="/useful-links/" style="display:none;">Useful Links</a>
                        <input id='ajaxButton' type="submit" onclick="imAnAjaxFunction(jQuery(this));" />
                        <?php

                                          // $m_id 
                                          // $unique
                                          // $row
                                          // $unique_lookup 
                                          // $fname 
                                          // $sname 
                                          // $city
                                          // $photo


                                          $c_email = esc_html( $current_user->user_email );
                                          $c_fname = esc_html( $current_user->user_firstname );
                                          $c_sname = esc_html( $current_user->user_lastname );



                              ?>
                        <script>
                            function imAnAjaxFunction($this) { //If you got error "Function ajax is not defined" declare this before document ready
                                var member_name = "<?php echo $fname." ". " ".$sname; ?>";
                                var member_id = "<?php echo $member_id; ?>";
                                var current_name = "<?php echo $c_fname." ". " ".$c_sname; ?>";
                                var current_email = "<?php echo $c_email; ?>";
                                var current_id = "<?php echo $user_id; ?>";
                                var get_date = jQuery('#date<?php echo $m_id ?>').val();
                                jQuery.ajax({
                                    method: "POST",
                                    context: this,
                                    async: false,
                                    url: "<?php echo esc_js( admin_url( 'admin-ajax.php' ) ) ?>",
                                    dataType: "text",
                                    data: {
                                        action: "email_mss_admins", // call the php function 
                                        member_name: member_name,
                                        member_id: member_id,
                                        current_name: current_name,
                                        current_email: current_email,
                                        current_id: current_id,
                                        get_date: get_date
                                    },
                                    success: function(response) {
                                        console.log(get_date);
                                        jQuery('#popup<?php echo $member_id ?> h3').fadeOut('slow', function() {
                                            jQuery('#popup<?php echo $member_id ?> h3').html("Thank You");
                                            jQuery('#popup<?php echo $member_id ?> h3').fadeIn('slow');
                                        });
                                        jQuery('#popup<?php echo $member_id ?> .pu-member').fadeOut('slow');
                                        jQuery('#popup<?php echo $member_id ?> .message').fadeOut('slow', function() {
                                            jQuery('#popup<?php echo $member_id ?> .message').html("my SwanSong will contact you in due course if we need to confirm any information.  On behalf of my SwanSong, we are sincerely sorry for your loss and during this difficult time, we have provided some links to some useful organisations that may provide support and comfort for you, your family and friends’.");
                                            jQuery('#popup<?php echo $member_id ?> .message').fadeIn('slow');
                                            jQuery('#popup<?php echo $member_id ?> #ajaxButton').fadeOut('slow', function() {
                                                jQuery('#popup<?php echo $member_id ?> #ajaxButton').html("Thank You");
                                                jQuery('#popup<?php echo $member_id ?> .useful').fadeIn('slow');
                                            });
                                        });
                                    }
                                });
                            }
                        </script>
                        <script type="text/javascript">
                            jQuery(document).ready(function() {
                                jQuery('#date<?php echo $member_id; ?>').datepicker({
                                    dateFormat: 'dd/mm/yy',
                                    maxDate: new Date
                                });
                            });
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
    <?php

            } else {
               // echo "Unique code mismatch (MU:".$unique_lookup." / U:".$unique." <br/>";
          }

          ?>
    <?php 
      }



}

return ob_get_clean();

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.