PHP 不从自定义字段中提取元数据

PHP not pulling metadata from custom fields

我正在开发一个允许通过自定义字段输入然后将其输出到不同页面的 WordPress 网站。当用户注册时,他们会通过向某人​​发送邀请 link 来请求某人成为 "trusted member",邀请 link 具有唯一标识符,当受邀者注册时,UID 将作为元数据存储在他们的帐户中。

然后使用(或应该)使用此元数据在页面上创建一个部分,显示有关邀请他们的人的一些信息。

(长话短说,该网站是一个纪念型网站,有人可以将他们的遗愿上传到该网站。受信任的验证者是指定的人,将其成员死亡通知网站所有者,以便网站所有者可以按照他们的意愿行事.)

基本上,我要做的是从用户帐户中获取元数据,然后使用它来获取人名。

我已将此函数的完整代码添加到代码笔中:https://codepen.io/david-wdc/pen/LYEoOXe

add_shortcode('trusted_verifier_dashboard', 'trusted_verifier_dashboard');

函数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();

    }`

(我添加了一条评论,其中包含它正在提取的一些元数据)

如果您需要我添加更多内容 information/code 或者您对此有任何疑问,请尽管提问!

谢谢

我已经设法解决了这个问题,

我试图将整个对象传递给一个函数,但它只需要对象中的一个元素。

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();

}