How to pass value in wordpress url
-
Hi all,
For example I use wordpress with this permalink statement
https://webisite.com/the-title-of-page
I want to pass one value in the URL (like $_GET[]) to be used later in [shortcode] FUNCTION which I created and called in page content ..
can I do that ??
-
yes you can… you can simply do like this https://www.yoursite.com/yourpost/?update=true&other=1 then parse that into your function or within the current page…
Using custom URL parameters in WordPress..
Sometimes when you are building WordPress sites you need to pass a parameter via the URL like this:
https://www.mysite.com?myvar=222
The problem is that WordPress is designed to reject any URL query parameters that it doesn’t recognise so your URL parameter will be dropped before you get a chance to use it.
[ Moderator Note: Please post code or markup snippets between backticks or use the code button. It makes your code readable. ]
The solution
Step 1We need to tell WordPress about the new parameter(s) we will be sending via the URL.
We want WordPress to recognise any url parameter sent in the format ‘https://www.yoursite.com?myvar=hello’
in any page on our WordPress site.The easiest way to do this is to create a WordPress plugin that uses a query filter to tell WordPress about new parameters.
Here’s the plugin code:[ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]
<?php /* Plugin Name: Parameter Plugin URI: https://webopius.com/ Description: A plugin to allow parameters to be passed in the URL and recognized by WordPress Author: Adam Boyse Version: 1.0 Author URI: https://www.webopius.com/ */ add_filter('query_vars', 'parameter_queryvars' ); function parameter_queryvars( $qvars ) { $qvars[] = ' myvar'; return $qvars; } ?>
The plugin is configured to add one new URL parameter name ‘myvar’ to WordPress.
Just copy the above code to a new .php file which you then copy to the plugins directory of your WordPress install.
You then need to activate the new plugin from within your WordPress admin screens.Step 2.
Now from any WordPress page that you can add code to (e.g. Theme page) or your own standalone page that is
WordPress aware you can use your variable like this:[ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]
global $wp_query; if (isset($wp_query->query_vars['myvar'])) { print $wp_query->query_vars['myvar']; }
Thank you for reply ..
iahnn, your solution doesn’t work ??
pwhstacy, I try your solution, but when I request page
the $wp_query->query_vars[‘myvar’] value printed as ‘0’are you have any way to fix this ?
Have you tried making sure you put it after the final slash at the end of the URL?
I am testing using the $_GET calls on a link such as:
https://wordpress/2012/08/22/hello-world/?custom_var=hello
As opposed to
https://wordpress/2012/08/22/hello-world?custom_var=hello
and it is working just fine.annedorko
I try $_GET method, but it’s not working ??
Can you post an example of a live link with the variable returning ‘0’, along with the snippet of code you’re using on that page?
That might help debug the issue ??
annedorko
When I was making a new fresh example for you (with english name for pages) because I’m working with arabic name examples, I found every thing went ok ^_^
I think my problem was with subpages, because with pages it works, but with subpages it doesn’t !!!
Thank you anyway for making me try for seconde time ^_^
No problem ?? Good luck! Hopefully you don’t need to make subpages work, but if you do perhaps we can look into it further.
sure, maybe when I have some time I’ll try to make it works with subpages
Thank you all
The problem is that WordPress is designed to reject any URL query parameters that it doesn’t recognise so your URL parameter will be dropped before you get a chance to use it.
In what context is this true? I have been passing variables in the URL for a long time now, and it does work.
same as me.. i am passing session data and post with no problems…
Of course you can get the variables. For example you create a form on top of every category page (which lists the posts of that category) and have it post back to the same page /?name=value. then you just use $_GET[‘name’] to get this variable. An complete example of using this (in combination with filtering the displayed posts based upon the parameter sent) can be found here:
https://infobak.nl/2012/09/12/using-add_filter-in-wordpress-to-filter-posts-shown-on-category-page/I wrote that article today, so feel free to criticize(<- wrong spelling?) it.
Its so simple to pass a uri value and get it from any specific page …
i have spent a lot of time on it and finally got the simple solution .Step 1)Pass the values from ur template/page/post as im passing
$link_anchor ='click here';$link ='https://example.com/?para1=param1_value/para2=para2_value';
$url= '' . $link_anchor . '';//im using here inside the template, u can simply use in your post/page as Click here
2)now you have the values in your url , its time to get those value
if you wants to get those values inside the post/ page , then simple instal the pluin called php exe .
other wise simply paste the code in your desired template file
<?php $uri = $_SERVER['REQUEST_URI'];
$elms = explode('/', $uri) ;
$value1 = $elms[1] ; // for the first parameter
$value2 = $elms[2] ; //for the 2nd parameter and so onim getting 2 values here…
3)now you have obtained your values fom url , now simply use them according to ur need..
thats it..
$_POST[] and &_GET[] perfectly works in wordpress. I dont find any Problem with that. Make sure to write it in a template and not in a post, or within any script where php scripts can fire.
- The topic ‘How to pass value in wordpress url’ is closed to new replies.