PluginProbe
URL Params / 2.1
URL Params v2.1
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.1, at urlparams.php

113 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.1
7 Author: Jeremy B. Shapiro
8 Author URI: http://www.asandia.com/
9 */
10
11 /*
12 URL Params (Wordpress Plugin)
13 Copyright (C) 2011-2016 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 $otheratts = "";
65 foreach($atts as $key => $val) {
66 $otheratts .= " $key=\"$val\"";
67 }
68
69 $return = "<$tagname $otheratts $return".($content ? ">$content</$tagname>" : "/>");
70 }
71 }
72
73 return $return;
74 }
75
76 /*
77 * If 'param' is found and 'is' is set, compare the two and display the contact if they match
78 * If 'param' is found and 'is' isn't set, display the content between the tags
79 * If 'param' is not found and 'empty' is set, display the content between the tags
80 *
81 */
82 function ifurlparam($atts, $content) {
83 $atts = shortcode_atts(array(
84 'param' => '',
85 'empty' => false,
86 'is' => false,
87 ), $atts);
88
89 $params = preg_split('/\,\s*/',$atts['param']);
90
91 foreach($params as $param)
92 {
93 if($_REQUEST[$param])
94 {
95 if($atts['empty'])
96 {
97 return '';
98 } elseif(!$atts['is'] or ($_REQUEST[$param] == $atts['is'])) {
99 return do_shortcode($content);
100 }
101 }
102 }
103
104 if ($atts['empty'])
105 {
106 return do_shortcode($content);
107 }
108
109 return '';
110 }
111
112 ?>
113