Yes! Your code can then retrieve the value with
$_GET['custom_var']. But you probably knew that, it’s just that a one word answer seemed wrong somehow.
thanks. do you know if there’s a way to grab the post-tag dynamically?
Do you think I could echo this variable in a form field by
<?php echo $view['form']->row($form['post_tags']) }} ?>
So you feel I should use the
$_GETfunction instead of echo?
<?php $_GET['custom_var'] ?>how can I get that into a form field?
$_GET is about the only reasonable way to extract a parameter passed in an URL with PHP. It can be used as an array variable just as any other. I like to assign the value to a more meaningful variable for better readability, but it is not required. I don’t know how you want to use this in a form, but for example you could do something like this:
<input type="text" name="<?php echo $_GET['custom_var'] ?>">
Which would end up being
<input type="text" name="hello">
in a browser, using your hello world example to pass “hello”
As for grabbing a post tag dynamically, it is usually possible. If you can provide an example of what you’re trying to grab and how it placed in in the page to begin with, I can provide a better answer.
wow big help, thanks!
Specifically, I’m trying to to put a button on a woocommerce product page, which will be a url to a page which has a search form where the row for product tags is pre-populated with the “product_tag” from the product page.
Maybe the url would be something like:
then I have to figure out some way to hard code in the $_GET function into the search form on the linked page. It’s not a wordpress search form btw. I’m thinking of maybe adding it a shortcode or something like that. The form is generated from a plugin but I’m not sure where to insert the $_GET function. ???
I think we each had a different mental image of what’s supposed to happen here, but I think I get where you’re going now.
The first issue is how to get the product page tags into your url. $_GET will not play into this part. You’ll have to look into how PHP generates the product page and how the product tags are output. They are possibly part of an array that was returned from a product query. In the simplest scenario, you may be able to generate your link on the product page template with something like
<a href="http://abc.com/search/?custom_var=<?php echo $product['tag'] ?>">Button</a>
Of course, it’s unlikely to be that easy, but I hope you get the idea.
Then you need
http://abc.com/search/to go to your search form. I’ll assume for now you got that figured out, we can chat more if need be.
Now we are on your search form page. This is where you use the $_GET super global array (not function!) to extract the url parameter and use it to pre-populate your form field like so
<input type="text" name="tag_search" value="<?php echo $_GET['custom_var'] ?>">
There may be some extra processing involved somewhere to convert the array element from the first part into a string suitable for the value attribute in this part, but that’s the root of it.
BTW, $_GET is a super global because you do not have to declare it as global. It is generated automatically by PHP from any URL parameters. It is automatically available to any code or function on the page that was requested, including any code that was included or required by that page.
One last issue is doing all this cleanly so it is not lost when you update woocommerce. I know woocommerce does provide some hooks for customizing certain things, use them if at all possible. If doing this cleanly is impossible, at least set it up so it is as easy as possible to reapply after an update.
- The topic ‘How can I pass post-tag variable with URL?’ is closed to new replies.