• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 789
  • Last Modified:

Wordpress custom URL variables

Hey Experts!!

I have a WP site that was using a legacy home grown CMS. Now we're converting it to 50% Wordpress and 50% Salesforce.  

We have a var say "myvar" that will be in the URL.  What I need to know is how can I use the variable in Wordpress?

So my URL: www.mysite.com/?myvar=82

Then in my page display things based on that var.  I hope I'm explaining that right

Thanks in advance!
0
LZ1
Asked:
LZ1
  • 6
  • 5
1 Solution
 
Ray PaseurCommented:
The URL variables will be present in a superglobal array named $_GET.  Superglobal variables are present and mutable in every scope and namespace.

From the example above:

$x = $_GET['myvar'];
echo $x;  // PRINTS 82

HTH, ~Ray
0
 
LZ1Author Commented:
So I just threw this in my page, however it's not showing up.

Will this work in WP?
0
 
Ray PaseurCommented:
Can you please post a URL that shows the page where it's not showing up?

WP has a lot of odd constructs in the way it creates its pages.  It may take a little more to see what's going on.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LZ1Author Commented:
The site isn't actually live yet. You can only get to it by modifying your host file.  

How about some code????
<?php
/**
 * Template Name: Artist Template
 * Description: Template for artist page
 */
	global $wp_query;
	if (isset($wp_query->query_vars['myvar']))
	{
		print $wp_query->query_vars['myvar'];
	}
//get_query_var( $qvars );


get_header(); ?>


<script src="http://api.html5media.info/1.1.5/html5media.min.js"></script>

<div class="middlecolumn" style="width:570px;">



			<!-- BEGIN: RELEASES CURRENT HEADER -->
			<div class="mainHeader"><a class="section-header" href="/artists"><img src="http://media.giantstep.net/globals/images/shared/h_artists_details.gif" alt="releases" border="0"></a></div>

			<div class="box">
			
								
				<table border="0" cellpadding="0" cellspacing="0" width="354">

				 <tbody><tr>

				  <td align="left" valign="middle" width="164"><img src="http://media.giantstep.net/assets/artists/detail/978.jpg" width="164" height="164" border="0" alt=""></td>
				  <td width="10"><img src="http://media.giantstep.net/globals/images/shared/spacer.gif" alt="GIANT STEP" width="10" height="5"></td>
				  <td align="left" valign="bottom" width="180">
				   <div class="bold bold23" style="padding-bottom:5px;">Lionel Loueke</div>
					<?php 
                        if (strpos($url, 'artists')) {
                            //echo "<h1>This is an artist page</h1>";
								$x = $_GET['myvar'];
								echo $x;  // PRINTS 82
                        }
                    ?>
                   <div class="bold greyLink"><a href="http://www.bluenote.com" target="_new">Blue Note</a></div>				   
				    <div style="height:10px;"><!-- --></div>
				   <div><audio src="http://media.giantstep.net/assets/mp3/Lionel%20Loueke/Heritage/Lionel%20Loueke%20-%2001%20Ife.mp3" controls=""></audio></div>
				   <div><a href="/forums/1500">Discuss in forums</a></div>
				   <div style="height:10px;"><!-- --></div>
				  
<!-- AddThis Button BEGIN -->
<script type="text/javascript">addthis_pub  = 'giantstep';</script>
<a href="http://www.addthis.com/bookmark.php" onmouseover="return addthis_open(this, '', '[URL]', '[TITLE]')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s9.addthis.com/button1-share.gif" width="125" height="16" border="0" alt=""></a><script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>
<!-- AddThis Button END -->

                  </td>

				 </tr>

				</tbody></table>
				
								
			</div>
				
						
			<div class="boxHeader">biography</div>
			<div class="box tallPadding">
			 <p class="copy">Starting out on vocals and percussion, Loueke picked up the guitar late, at age 17. After his initial to exposure to jazz in Benin, he left to attend the National Institute of Art in nearby Ivory Coast. In 1994 he left Africa to pursue jazz...&nbsp;<a href="/artists/978/bio/">more</a></p>

			 			 <br>
			 <a href="/artists/978/bio/">More About Lionel Loueke</a>
			 			  			 | <a href="http://www.lionelloueke.com/" target="_new">Official Site</a>
			  			 | <a href="http://facebook.com/lionelloueke" target="_new">Facebook</a>
			  			 			 </div>
												
			 

			<div class="boxHeader">releases</div>
			
			 
				<div class="imageList greyRollover">
					  <div class="coverimage"><a href="/releases/3088"><img src="http://media.giantstep.net/assets/releases/detail/3088.jpg" width="164" height="164" border="0" alt=""></a></div>
					  <div class="blurb greyText">
						   <div class="bold"><a href="/releases/3088">Heritage</a></div>
						   <div class="bold blackLink"><a href="/artists/978">Lionel Loueke</a></div>						   <div class="" style="padding-top:5px;"><strong>Album</strong>&nbsp;&nbsp;&nbsp;<a href="/releases/3088">info</a> | <a href="http://itunes.apple.com/us/album/heritage/id550704736" target="_new">buy</a><br></div>
						   <div class="releaseDate">Released <strong>Aug 28, 2012</strong></div>
						   <div class="releaseBy">Released By Blue Note</div>
					  </div>
					  <div class="clear"><!-- --></div>
				</div>

			 
				<div class="imageList greyRollover">
					  <div class="coverimage"><a href="/releases/2505"><img src="http://media.giantstep.net/assets/releases/detail/2505.jpg" width="164" height="164" border="0" alt=""></a></div>
					  <div class="blurb greyText">
						   <div class="bold"><a href="/releases/2505">Mwaliko</a></div>
						   <div class="bold blackLink"><a href="/artists/978">Lionel Loueke</a></div>						   <div class="" style="padding-top:5px;"><strong>CD</strong>&nbsp;&nbsp;&nbsp;<a href="/releases/2505">info</a> | <a href="http://www.amazon.com/gp/product/B0031S4K7M?ie=UTF8&amp;tag=giaste-20&amp;linkCode=xm2&amp;camp=1789&amp;creativeASIN=B0031S4K7M" target="_new">buy</a><br></div>
						   <div class="releaseDate">Released <strong>Feb 9, 2010</strong></div>
						   <div class="releaseBy">Released By Blue Note Records</div>
					  </div>
					  <div class="clear"><!-- --></div>
				</div>

			 
				<div class="imageList greyRollover">
					  <div class="coverimage"><a href="/releases/1826"><img src="http://media.giantstep.net/assets/releases/detail/1826.jpg" width="164" height="164" border="0" alt=""></a></div>
					  <div class="blurb greyText">
						   <div class="bold"><a href="/releases/1826">Karibu</a></div>
						   <div class="bold blackLink"><a href="/artists/978">Lionel Loueke</a></div>						   <div class="" style="padding-top:5px;"><strong>CD</strong>&nbsp;&nbsp;&nbsp;<a href="/releases/1826">info</a> | <a href="http://www.amazon.com/gp/product/B00140GXKG/104-8902378-1329505?ie=UTF8&amp;tag=giaste-20&amp;linkCode=xm2&amp;camp=1789&amp;creativeASIN=B00140GXKG" target="_new">buy</a><br></div>
						   <div class="releaseDate">Released <strong>Mar 25, 2008</strong></div>
						   <div class="releaseBy">Released By Blue Note</div>
					  </div>
					  <div class="clear"><!-- --></div>
				</div>

			 			 
							
			<div class="footer"><a href="javascript:history.go(-1)">return to main listing</a></div>

</div>


<?php get_footer(); ?>

Open in new window

0
 
Ray PaseurCommented:
On line 40, the access to $_GET is inside a conditional (if() statement).  Try moving it to some place in the code that will get executed unconditionally.  You should see the output if these two instructions run.
0
 
LZ1Author Commented:
Ok, so now I'm confused. I don't think WP is picking up the $_GET.

This works:

<?php 
     global $wp_query;
          if (isset($wp_query->query_vars['myvar']))
	     {
		print $wp_query->query_vars['myvar'];
	     }
                    ?>

Open in new window


But this doesn't:
$x = $_GET['myvar'];
echo $qvars;  // PRINTS 82
	

Open in new window

0
 
Ray PaseurCommented:
The reason the second snippet does not do what you want is that the value in $_GET['myvar'] is assigned to the variable $x.  Then on the next line, instead of echoing $x the code echoes $qvars
0
 
LZ1Author Commented:
That was my fault entirely Ray. I never changed it. Ok, so both work now.

Is there any advantage going with one way over the other in WP?
0
 
Ray PaseurCommented:
Good, glad it's working.

WP is a fairly intricate CMS.  As long as you have the variables from $_GET, the way you would use those variables would be up to you.  Consider this statement:

$wp_query->query_vars['myvar']

That is an object-oriented notation.  It says, "Use the object that is stored in the $wp_query variable.  With that object, return the array position in the query_vars property that is indexed by the literal string 'myvar.'  What I cannot tell you without research is what else WP might have been doing with 'myvar' before it stored it in the query_vars array inside the $wp_query object.  It might have filtered the external input, etc.  These are usually the things that programmers write as part of their applications.  When you start with a pre-packaged app like WP, there is a learning curve, not only for PHP but also for WP.
0
 
LZ1Author Commented:
Ok perfect. I know that seemed kind of remedial, but I will definitely have more questions.

Thanks Ray!
0
 
Ray PaseurCommented:
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now