提交表单后,我在我的 function.php 中使用 query_vars 来获取我的 URL 中的一些数据。这一切都很好,但是我觉得代码可以改进很多,但我不确定如何重构它。
function add_query_vars(
$membership_vars
) {
$membership_vars[] = "firstName";
$membership_vars[] = "membership";
$membership_vars[] = "subscription";
$membership_vars[] = "total";
return $membership_vars;
}
add_filter('query_vars', 'add_query_vars');
然后在自定义页面模板(PHP)中,我在页面上显示如下:
$firstName = urldecode($wp_query->query_vars['firstName']);
$membership = urldecode($wp_query->query_vars['membership']);
$subscription = urldecode($wp_query->query_vars['subscription']);
$total = urldecode($wp_query->query_vars['total']);
<section>
<p>Congratulations <?php echo $firstName ?>!</p>
<p>You have chosen: <?php echo $membership ?>
<p>You have subscribed to: <?php echo $subscription ?>
<p>Your total paid is: <?php echo $total ?>
</section>
如您所见,代码不是 DRY,但是如前所述,我不确定如何在 PHP 中重构它。