How to Pass Values Between Pages in WordPress

How to Pass Values Between Pages in WordPress thumbnail
Pass values between pages using the "Get" method in PHP.

WordPress is built on PHP and MySQL. Anything you can do in a regular PHP website, you can also do in WordPress. For this reason, if you want to pass values between pages, use the "Get" method of PHP. The "Get" method looks for variables passed through the URLs of Web pages and grabs them for use on other pages. The easiest way to pass a variable into the URL of a page is creating a link with the variable tacked on to its end. Adding the "Get" method to the page.php template will allow that page to grab the passed value.

Instructions

    • 1

      Log into the WordPress dashboard at the wp-admin directory of your website. Navigate to "Appearance" and click the "Editor" link to load the "Edit Themes" screen. Click the link to page.php listed under the "Templates" heading.

    • 2

      Add the following code where you want to output your passed value:

      <?php if(isset($_GET['myvar'])) { echo $_GET['myvar']; } ?>

      The above code looks for the variable "myvar" in the page URL. If the variable is present, this code will output its variable onto the page. Click the blue "Update File" button to save your changes.

    • 3

      Navigate to "Pages" and edit or add a page. Type the text for a link in the editor box. Highlight the link text and click the "Insert/edit link" button. Click "Or link to existing content" and select a page from the list.

    • 4

      Add "?myvar=Hello" to the end of the URL in the "URL" field of the "Insert/edit link" box. Change "Hello" to the value you want to pass to the linked page. Click the blue "Update" button to finish creating your link. Update or publish the page to save your work. When you click on the link you created, it will take you to that page, where you will see the value of "myvar" printed on the screen.

Tips & Warnings

  • You can also change "echo $_GET['myvar'];" to "$myvar = $_GET['myvar'];" if you want to pass the variable without outputting its variable.

  • Pass multiple values by chaining them with ampersands like this: "/?myvar=Hello&name=John".

  • Back up your theme files before editing them. If you encounter a "white screen" error, re-upload the theme to get back in to WordPress.

  • Do not pass the value of a password or other sensitive information using the "Get" method. Site visitors will see the values in the Web address bars of their browsers.

Related Searches:

References

  • Photo Credit Jupiterimages/Brand X Pictures/Getty Images

Comments

You May Also Like

Related Ads

Featured