Ready to get started?Download WordPress


Pass end of URL as $_GET variable to template file (5 posts)

  1. Hugh Grigg
    Posted 3 years ago #

    I'm sure this must be fairly simple but can't figure out how to do it.

    I have a page with it's own template, say mysite.com/custom. This page is handled by custom.php.

    How can I set it up so that mysite.com/custom/anything is handled by custom.php?var=anything

    I just need whatever comes after custom/ passing to custom.php as a string, no matter what it is.

  2. Frumph
    Posted 3 years ago #

    If say your trying to find a variable being passed to it


    There are actually several different methods to do this, you can find and validate the $_REQUEST

    for example:

    if (isset($_REQUEST['somevar'])) $somevar = esc_attr($_REQUEST['somevar']);

    Or the WP prefered method where you check if the get_query_var has it.

    I just do this:

    if (isset($wp_query->query_vars['somevar'])) $somevar = esc_attr($wp_query->query_vars['somevar']);
  3. Hugh Grigg
    Posted 3 years ago #

    Thanks for your response. Sorry, I didn't explain what I meant properly.

    The problem is not for custom.php to find variables that have been passed to it. It's getting mysite.com/custom/any-random-stuff/with-slashes-etc to be handled by custom.php?var=any-random-stuff/with-slashes-etc

  4. Frumph
    Posted 3 years ago #

    Ah you need to create some rewrite rules to handle those


    This allows you to create a new ruleset to use with custom permalinks

  5. Hugh Grigg
    Posted 3 years ago #

    Thanks again. I'm still a bit unclear on the usage of WP Rewrite. Do the functions there need to be run once, to update wordpress' rewrite rules? Where should they be run from?

Topic Closed

This topic has been closed to new replies.

About this Topic