Skip Navigation

[Resolved] How to retrieve URL parameters for use inside a PHP template

This thread is resolved. Here is a description of the problem and solution.

Problem:
Client is directly editing a PHP template and wants to retrieve URL parameters to use in that template.

Solution:
They are available in the global $_GET PHP variable.

Relevant Documentation:
http://php.net/manual/en/reserved.variables.get.php

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 7.29 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by stuart 6 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#604335
chrome_2018-01-08_23-19-49.png

I am trying to build a dynamic <title> </title> and to help me to do this I'd like to use a [wpv-search-term param='c'] inside a function (functions.php of a child theme)

I have this working in a redementry type of way but am hoping there is a more logical way by referencing the actual wpv-search-term custom field and the parameter in the function so I can use multiple parameters to build a dynamic title.

This is what I am doing so far - and it work with one parameter, but with more in the querystring it renders the first part then the second it just ads ?c=whatever.

(please dont laugh at my poor skills, its the best i could do with google and hacking together a lot of peoples different problems).

add_filter( 'document_title_parts', 'dynamic_custom_title');
function dynamic_custom_title( $title ) {
if ( ! is_singular() ) return $title;
$custom_title = trim(get_post_meta( get_the_id(), 'wpcf-dynamic-title', true ));
// custom field called wpcf-dynamic-title - this has a shortcode in it [wpv-search-term param='c']
if( ! empty( $custom_title ) )
$custom_title2 = trim(get_post_meta( get_the_id(), 'wpcf-dynamic-title2', true ));
// custom field called wpcf-dynamic-title2 - this has a shortcode in it [wpv-search-term param='i']
if( ! empty( $custom_title2 ) )
{
$custom_title = do_shortcode($custom_title);
// this enables the shortcode in the custom field: wpcf-dynamic-title
$custom_title2 = do_shortcode($custom_title2);
// this enables the shortcode in the custom field: wpcf-dynamic-title2
$title['title'] = $custom_title . " " . $custom_title2;
// this add the two together with a space
}
return $title;
}

-----------

URL: hidden link
Field: wpcf-dynamic-title
value entered is: [wpv-search-term param='c']

Field: wpcf-dynamic-title2
value entered is: [wpv-search-term param='i']

This should return:

<title>cheese burger </title

But returns:

<title>cheese?i=burger </title>

---------

Id say a better way would be to use the values of the fields (wpv-search-term) directly rather than entering shortcodes in to custom fields and then rendering them... but I dont have a clue how to do that... and this is the best i could do.

Any help - appreciated as ever ?

Cheers Stu

#604360

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Stuart

If you are writing some custom PHP and want to retrieve values from URL parameters then I would bypass using Views shortcodes for this altogether.

The URL parameters are available via the PHP global $_GET request variable.

Here is a tiny bit of of code I added to page.php to show $_GET and the log results when I visit the sample page of a default WordPress install after including some URL parameters:

// I added this to the top of page.php before the get_header() call
error_log(print_r($_GET, true));

// I then visited this url
// site.com/sample-page/?food=burger&drink=beer

// this is what was sent to my debug.log
[08-Jan-2018 13:23:13 UTC] Array
(
    [food] => burger
    [drink] => beer
)

The URL parameters can be readily grabbed from the $_GET global so you don't need to involve Views shortcodes.

I don't know what exactly you are aiming to do but when you come to use this you will want to test to see if the URL parameters exist before using them, e.g.

if ( isset( $_GET['food'] ) {
    $food = $_GET['food'];
}
#605623

It was me making a cock up all along - it worked perfectly

I had used the querystring incorrectly - hidden link (<-- not the second "?" and no "&")

it should have been: hidden link <-- this works fine.

This means I can now dynamically change the <title></title> of a page based on the URL parameters with shortcodes added to a custom field.

I want to do this for SEO reasons, I have a large number of views that pass URL parameters to the CPTs and want to create titles, H1s etc to match correctly to what the page displays with the view.

This is not possible with SEO plugins due to the way the custom field data is taken from Types.

E.g

/nsw/?category=Gardners&state=NSW&city=Sydney

<title>Gardners in Sydney NSW Australia</title>

In the custom field of the CPT (nsw):

[wpv-search-term param="category"] in [wpv-search-term param="city"] [wpv-search-term param="state"] [types field='country'][/types]

Then the view can pull al the wpv-search-terms to display the data correctly on the page.

? awesome!

---- AND----

.... Now this is one step further (no idea how this effects performance etc)

you can add a shortcode to a content template instead of a custom field shortcode...

E.g:

in the custom field of the custom post type:
[wpv-post-body view_template="injection"]

And the content template can check to see if the URL parameters are present etc etc

[wpv-conditional if="( '[wpv-search-term param='c']' ne '' )"]

[wpv-search-term param="c"] [wpv-search-term param="i"] [types field='country'][/types]

[/wpv-conditional]
Empty

so this will look at the URL - if the c= has a value - if not mark the <title> Empty </title>
hidden link

If not it will use the parameter value to build the <title></title>