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

125 lines 3.5 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.4
7 Author: Jeremy B. Shapiro
8 Author URI: http://www.asandia.com/
9 */
10
11 /*
12 URL Params (Wordpress Plugin)
13 Copyright (C) 2011-2023 Jeremy Shapiro
14
15 */
16
17 // error_reporting(E_ALL); // Helpful for checking for warnings that are TYPICALLY hidden but may be present on some installs
18
19 // Tell WordPress to register the shortcodes
20 add_shortcode("urlparam", "urlparam");
21 add_shortcode("ifurlparam", "ifurlparam");
22
23 function urlparam($attributes, $content) {
24 $defaults = array(
25 'param' => '',
26 'default' => '',
27 'dateformat' => '',
28 'attr' => '',
29 'htmltag' => false,
30 );
31
32 // 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.
33 $attributes = array_merge($defaults, $attributes);
34
35 $params = preg_split('/,\s*/',$attributes['param']);
36
37 $return = false;
38
39 foreach($params as $param)
40 {
41 if(!$return
42 && array_key_exists($param, $_REQUEST)
43 && ($rawText = $_REQUEST[$param])
44 ) {
45 if(($attributes['dateformat'] != '')
46 && strtotime($rawText)
47 ) {
48 $return = date($attributes['dateformat'], strtotime($rawText));
49 } else {
50 $return = esc_html($rawText);
51 }
52 }
53 }
54
55 if(!$return) {
56 $return = $attributes['default'];
57 }
58
59 if(($attr = $attributes['attr'])
60 // Don't allow setting attributes for javascript events, i.e. onclick
61 && !preg_match('/^on/', $attr)
62 ) {
63 $return = ' ' . $attr . '="' . $return . '" ';
64
65 if($attributes['htmltag']) {
66 $tagName = $attributes['htmltag'];
67
68 foreach(array_keys($defaults) as $key) {
69 unset($attributes[$key]);
70 }
71 $otherAttributes = "";
72 foreach($attributes as $key => $val) {
73 // Don't allow setting attributes for javascript events, i.e. onclick
74 if(!preg_match('/^on/', $key)) {
75 $otherAttributes .= " $key=\"$val\"";
76 }
77 }
78
79 $return = "<$tagName $otherAttributes $return".($content ? ">$content</$tagName>" : "/>");
80 }
81 }
82
83 return $return;
84 }
85
86 /*
87 * If 'param' is found and 'is' is set, compare the two and display the contact if they match
88 * If 'param' is found and 'is' isn't set, display the content between the tags
89 * If 'param' is not found and 'empty' is set, display the content between the tags
90 *
91 */
92 function ifurlparam($attributes, $content) {
93 $attributes = shortcode_atts(array(
94 'param' => '',
95 'empty' => false,
96 'is' => false,
97 ), $attributes);
98
99 $params = preg_split('/,\s*/',$attributes['param']);
100
101 foreach($params as $param)
102 {
103 if($_REQUEST[$param])
104 {
105 if($attributes['empty'])
106 {
107 return '';
108 } elseif(!$attributes['is']
109 || ($_REQUEST[$param] == $attributes['is'])
110 ) {
111 return do_shortcode($content);
112 }
113 }
114 }
115
116 if ($attributes['empty'])
117 {
118 return do_shortcode($content);
119 }
120
121 return '';
122 }
123
124 ?>
125