Skip to content

Instantly share code, notes, and snippets.

@martynchamberlin
Last active December 20, 2015 23:29
Show Gist options
  • Save martynchamberlin/6212365 to your computer and use it in GitHub Desktop.
Save martynchamberlin/6212365 to your computer and use it in GitHub Desktop.
Shortcode for retrieving URL variables within WordPress
<?php
/**
* Insert this code inside your functions.php file. Do not have the opening <?php tag, as this should
* already exist at the top of your functions file.
*
* USAGE: If you have firstname as a variable in your page's URL (e.g. http://mydomain.com?firstname=Martyn)
* then putting [get var=firstname] into a WordPress post type or widget would print "Martyn" to the screen.
* After using exec PHP plugins for months (i.e. <?php echo $_GET['firstname']; ?>) I'm moving away from
* executing PHP directly in posts/pages and widegets. This is the whole reason shortcodes were invented in the
* first place. Enjoy!
**/
function get( $var )
{
extract( shortcode_atts( array(
'var' => ''
), $var ) );
// referring to the Product Class
return $_GET[$var];
}
add_shortcode( 'get', 'get' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment