skip to Main Content

I am using query_vars in my function.php to get some data in my URL after a form is submitted. This is all working fine, however I feel like the code can be improved a lot, but I’m not sure how to refactor this.

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

And then in a custom page template (PHP) I am displaying this on the page like this:

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

As you can see the code isn’t DRY, however as mentioned I’m not sure how to refactor this in PHP.

2

Answers


  1. What about using a custom filter to define your query vars?

    In your plugin/functions.php you can do:

    add_filter('PRFX_membership_vars', 'PRFX_default_membership_vars', 10,1);
    
    function PRFX_default_membership_vars($vars) {
        return ['firstName', 'membership', 'subscription', 'total'];
    }
    
    
    
    add_filter('query_vars', 'PRFX_add_query_vars');
    
    function PRFX_add_query_vars($vars) {
    
        $membership_vars = apply_filters('PRFX_membership_vars', []);
    
        foreach ($membership_vars as $membership_var) {
            $vars[] = $membership_var;
        }
        return $vars;
    }
    

    While in your custom template:

    $membership_vars = apply_filters('PRFX_membership_vars', []);
    $membership_values = [];
    
    
    foreach ($membership_vars as $membership_var) {
        $membership_values[$membership_var] = urldecode( get_query_var($membership_var, '') );
    }
    
    
    <section>
      <p>Congratulations <?php echo $membership_values['firstName'] ?>!</p>
      <p>You have chosen: <?php echo $membership_values['membership'] ?> 
      <p>You have subscribed to: <?php echo $membership_values['subscription'] ?> 
      <p>Your total paid is: <?php echo $membership_values['total'] ?> 
    </section>
    
    Login or Signup to reply.
  2. Maybe you can try something like this:

    function add_query_vars( $membership_vars ) {
     $params = array( 'firstName', 'membership', 'subscription', 'total' );
     
     foreach ( $params as $param ) {
       $membership_vars[] = $param;
     }
    
     return $membership_vars;
    }
    
    add_filter('query_vars', 'add_query_vars');
    

    and then in the template:

    $params = array( 'firstName', 'membership', 'subscription', 'total' );
    $values = array();
    
    foreach ( $params as $param ) {
      $values[$param] = urldecode( get_query_var($param, '') );
    }
    
    <section>
     <p>Congratulations <?php echo esc_html( $values['firstName'] ); ?>!</p>
     <p>You have chosen: <?php echo esc_html( $values['membership'] );?> 
     <p>You have subscribed to: <?php echo esc_html( $values['subscription'] ); ?> 
     <p>Your total paid is: <?php echo esc_html( $values['total'] ); ?> 
    </section>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search