PluginProbe
URL Params / 2.0
URL Params v2.0
trunk 1.6 1.7 1.8 2.0 2.1 2.2 2.3 2.4 2.5 2.5-review
url-params / urlparams.php

urlparams.php in URL Params 2.0, at urlparams.php

110 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: URL Params
4 Plugin URI: http://asandia.com/wordpress-plugins/urlparams/
5 Description: Short Code to grab any URL Parameter
6 Version: 2.0
7 Author: Jeremy B. Shapiro
8 Author URI: http://www.asandia.com/
9 */
10
11 /*
12 URL Params (Wordpress Plugin)
13 Copyright (C) 2011-2015 Jeremy Shapiro
14
15 */
16
17 //tell wordpress to register the shortcodes
18 add_shortcode("urlparam", "urlparam");
19 add_shortcode("ifurlparam", "ifurlparam");
20
21 function urlparam($atts, $content) {
22 $defaults = array(
23 'param' => '',
24 'default' => '',
25 'dateformat' => '',
26 'attr' => '',
27 'htmltag' => false,
28 );
29
30 # we used to use shortcode_atts(), but that would nuke an extra attributes that we don't know about but want. array_merge() keeps them all.
31 $atts = array_merge($defaults, $atts);
32
33 $params = preg_split('/\,\s*/',$atts['param']);
34
35 $return = false;
36
37 foreach($params as $param)
38 {
39 if(!$return and ($rawtext = $_REQUEST[$param]))
40 {
41 if(($atts['dateformat'] != '') && strtotime($rawtext))
42 {
43 $return = date($atts['dateformat'], strtotime($rawtext));
44 } else {
45 $return = esc_html($rawtext);
46 }
47 }
48 }
49
50 if(!$return) {
51 $return = $atts['default'];
52 }
53
54 if($atts['attr']) {
55 $return = ' ' . $atts['attr'] . '="' . $return . '" ';
56
57 if($atts['htmltag']) {
58 $tagname = $atts['htmltag'];
59
60 foreach(array_keys($defaults) as $key) {
61 unset($atts[$key]);
62 }
63
64 $return = "<$tagname ".
65 implode(' ', array_map(function($key, $val) { return "$key=\"".$val."\""; }, array_keys($atts), $atts)).
66 $return.($content ? ">$content</$tagname>" : "/>");
67 }
68 }
69
70 return $return;
71 }
72
73 /*
74 * If 'param' is found and 'is' is set, compare the two and display the contact if they match
75 * If 'param' is found and 'is' isn't set, display the content between the tags
76 * If 'param' is not found and 'empty' is set, display the content between the tags
77 *
78 */
79 function ifurlparam($atts, $content) {
80 $atts = shortcode_atts(array(
81 'param' => '',
82 'empty' => false,
83 'is' => false,
84 ), $atts);
85
86 $params = preg_split('/\,\s*/',$atts['param']);
87
88 foreach($params as $param)
89 {
90 if($_REQUEST[$param])
91 {
92 if($atts['empty'])
93 {
94 return '';
95 } elseif(!$atts['is'] or ($_REQUEST[$param] == $atts['is'])) {
96 return do_shortcode($content);
97 }
98 }
99 }
100
101 if ($atts['empty'])
102 {
103 return do_shortcode($content);
104 }
105
106 return '';
107 }
108
109 ?>
110