10

I'd like to say something on my page based on what page the person got there from. I can set up the link to that page to be:

www.example.com/page?parameter=myparameter

Is there a way I can access that parameter so that I can have it show on the page, somewhere in my content?

2
  • 1
    Sorry I understand that the answers are examples, but I find it important to note that they should not be used as-is. User input (like query string parameters) should never be echoed unsanitized to prevent XSS attacks! All answers so far are vulnerable to XSS and should at least include a warning.
    – kubi
    Commented Aug 14, 2019 at 8:43
  • For a SAFE way to implement this, use this plugin - or browse the code of the plugin on github.
    – 1NN
    Commented Oct 12, 2022 at 10:15

3 Answers 3

6

Per the OP's answer within their question:

With your help and some Googling, I put together a shortcode that returns the value of any url parameter. Here it is:

//THIS IS A CUSTOM SHORTCODE TO DISPLAY A PARAMETER FROM THE URL
function URLParam( $atts ) {  
    extract( shortcode_atts( array(
        'param' => 'param',
    ), $atts ) );
    return $_GET[$param];  
}
add_shortcode('URLParam', 'URLParam'); 

I use the shortcode like this to get the trip_type from my URL:

[URLParam param='trip_type']
1
  • This helped me, thanks! How do I get values from form post data instead of query string?
    – Amjad
    Commented May 22, 2018 at 4:04
4

You could use a PHP session, look in the query object for the request, or use $_GET with your url, but it's not for sensitive information...

Something like this;

Grab the trip name from the URL

$tripname = $_GET['tripname'];

Use it in your heading;

if(isset($tripname)) echo '<h1>Plan Your Trip To ' .$tripname. '</h1>';

16
  • Thanks! This is not sensitive info. How would I go about doing that?
    – user3515
    Commented Aug 23, 2013 at 15:41
  • This might help you out : w3schools.com/php/php_get.asp
    – Dan
    Commented Aug 23, 2013 at 15:42
  • Thanks! Can I put that straight onto the page, or does it need to go into a template file?
    – user3515
    Commented Aug 23, 2013 at 15:43
  • Depends where you need it - if it's only showing on a page that runs on one template, you would put it there. Where is the information coming from, and where will it end up?
    – Dan
    Commented Aug 23, 2013 at 15:49
  • The story is like this: I have a lot of 'trip pages', each of which has information about a certain trip we run. Then, they click "register", and it takes them to a page with a registration form (gravity forms). I'd like the page (or else the description on the top of the form) to say "Fill out this form to register for the xxxx trip".
    – user3515
    Commented Aug 23, 2013 at 15:55
2

Here's another great way to accomplish this. This won't require shortcodes and will automatically update each page you create where there parameter "trip" is present!

1) Navigate to your theme folder and open functions.php (or create it if it's missing)

2) Add the following code and save:

<?php 
    add_filter( 'the_content', 'my_trip_filter' );

    function my_trip_filter( $content ) {
        if ( isset($_REQUEST['trip']) && is_single() ) {
            $content = "<h1>Fill out this form to register for the " . $_REQUEST['trip'] . " trip</h1>" . $content;
        }

        return $content;

    }
?>

This code uses the_content filter to override each time you use the function the_content() function to print your blog information. It detects whether or not the page requested is a single page/post and not the archive. Additional logic or overrides can be added to it. Just make sure you return $content and whatever message you desire will be returned to the screen.

Enjoy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.