Ad

PHP Not Pulling Metadata From Custom Fields

- 1 answer

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" target="_blank" rel="nofollow noreferrer" 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" target="_blank" rel="nofollow noreferrer" 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" target="_blank" rel="nofollow noreferrer" 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

Ad

Answer

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" target="_blank" rel="nofollow noreferrer" 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" target="_blank" rel="nofollow noreferrer" 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" target="_blank" rel="nofollow noreferrer" 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();

}
Ad
source: stackoverflow.com
Ad