Last active
December 20, 2015 23:29
-
-
Save martynchamberlin/6212365 to your computer and use it in GitHub Desktop.
Shortcode for retrieving URL variables within WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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