Configuration guides for IT Administrators

WordPress – How to pass variable as parameter in URL

WordPress – How to pass variable as parameter in URL

 

This simple method worked for my WordPress website to pass a parameter in URL. In this tutorial, the Datewise Invoices page lists down all the invoices created between the dates. When you click on a specific Invoice No, the invoice no is passed as parameter value and a new webpage opens up more details of the invoice.

 

Step 1: Add this code to your theme’s functions.php file

function add_query_vars_filter( $vars )
{
$vars[] = “parvar”;
return $vars;
}
add_filter( ‘query_vars’, ‘add_query_vars_filter’ );

 

Step 2: Now parvar variable is available in all WordPress pages for you to query and show the results.

if (isset($wp_query->query_vars[‘parvar’]))
{
$invoiceno = $wp_query->query_vars[‘parvar’];
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Lucas Brey

I’m Lucas Brey, a travel blogger sharing practical guides, hidden gems, and honest tips from the road. Follow along for smart itineraries, great food finds, and stories worth bookmarking.

Tags