Allow PHP in Posts and Pages - Version 1.0

Version Description

Download this release

Release Info

Developer Hit Reach
Plugin Icon wp plugin Allow PHP in Posts and Pages
Version 1.0
Comparing to
See all releases

Version 1.0

Files changed (2) hide show
  1. README.txt +77 -0
  2. allowphp.php +140 -0
README.txt ADDED
@@ -0,0 +1,77 @@
1
+ === PHP Shortcode ===
2
+ Contributors: Hit Reach
3
+ Donate link:
4
+ Tags: post, pages, posts, code, php
5
+ Requires at least: 2.5
6
+ Tested up to: 3.0.1
7
+ Stable tag: 1.0
8
+
9
+ Allow PHP in posts and pages allows you to add php functionality to Wordpress Posts and Pages
10
+
11
+ == Description ==
12
+
13
+ Allow PHP in posts and pages adds the functionality to include PHP in wordpress posts and pages by adding a simple shortcode [php].code.[/php]
14
+
15
+ This plugin strips away the automatically generated wordpress <p> and <br/> tags but still allows the addition of your own <p> and <br/> tags
16
+
17
+ == Usage ==
18
+
19
+ To add the PHP code to your post or page simply place any PHP code inside the shortcode tags.
20
+
21
+ For example: If you wanted to add content that is visible to a particular user id:
22
+
23
+
24
+
25
+ [php]
26
+ global $user_ID;
27
+ if($user_ID == 1){
28
+ echo "Hello World";
29
+ }
30
+ [/php]
31
+
32
+
33
+ This code will output Hello World to only user id #1, and no one else
34
+
35
+ in addition, should this code not be working (for example a missing ";") simply just change the [php] to be [php debug=1]
36
+
37
+
38
+ [php debug=1]
39
+ global $user_ID;
40
+ if($user_ID == 1){
41
+ echo "Hello World"
42
+ }
43
+ [/php]
44
+
45
+
46
+ Will result in the output:
47
+
48
+
49
+ Parse error: syntax error, unexpected '}', expecting ',' or ';' in XXX : eval()'d code on line 5
50
+ global $user_ID;
51
+ if($user_ID == 1){
52
+ echo "Hello World"
53
+ }
54
+
55
+
56
+ == Some Important Notes ==
57
+
58
+ This plugin strips away all instances of <p> and <br /> therefore code has been added so that if you wish to use tags in your output (e.g.):
59
+ [php]
60
+ echo "hello <br /> world";
61
+ [/php]
62
+
63
+
64
+ the < and > tags will need to be swapped for [ and ] respectively so <p> becomes [p] and </p> becomes [/p] which is converted back to <p> at runtime. these [ ] work for all tags (p, strong, em etc.).
65
+ [php]
66
+ echo "hello [br /] world";
67
+ [/php]
68
+
69
+ == Installation ==
70
+
71
+ 1. Extract the zip file and drop the contents in the wp-content/plugins/ directory of your WordPress installation
72
+ 2. Activate the Plugin from Plugins page
73
+
74
+ == Misc ==
75
+ Developed by <a href='http://www.hitreach.co.uk' target="_blank" style='text-decoration:none;'>Hit Reach</a>
76
+ Check out our other <a href='http://www.hitreach.co.uk/services/wordpress-plugins/' target="_blank" style='text-decoration:none;'>Wordpress Plugins</a>
77
+ Version: 1.0 <a href='http://www.hitreach.co.uk/services/wordpress-plugins/allow-php-in-posts-and-pages/' target="_blank" style='text-decoration:none;'>Support &amp; Comments</a>
allowphp.php ADDED
@@ -0,0 +1,140 @@
1
+ <?php
2
+ /*
3
+ Plugin Name: Allow PHP in posts and pages
4
+ version: 1.0
5
+ Plugin URI: http://www.hitreach.co.uk/wordpress-plugins/allow-php-in-posts-and-pages/
6
+ Description: Allow PHP in posts and pages allows you to add php functionality to Wordpress Posts and Pages whilst still retaining HTML tags
7
+ Author: Jamie Fraser - Hit Reach
8
+ Author URI: http://www.hitreach.co.uk/
9
+ */
10
+ add_shortcode('php','php_handler');
11
+ add_action('admin_menu', 'allow_php_menu');
12
+
13
+ function php_handler($args, $content=null){
14
+ global $is_comment;
15
+ if($is_comment){return "";}
16
+ extract( shortcode_atts(array('debug' => 0), $args));
17
+ if($args['debug'] != 1){
18
+ error_reporting(E_NONE);
19
+ }
20
+
21
+ $content =(htmlspecialchars($content,ENT_QUOTES));
22
+ $content = str_replace("&amp;#8217;","'",$content);
23
+ $content = str_replace("&amp;#8216;","'",$content);
24
+ $content = str_replace("&amp;#8220;","\"",$content);
25
+ $content = str_replace("&amp;#8221;","\"",$content);
26
+ $content = str_replace("&amp;#8243;","\"",$content);
27
+ $content = str_replace("&amp;#039;","'",$content);
28
+ $content = str_replace("&amp;lt;br /&amp;gt;"," ", $content);
29
+ $content = htmlspecialchars_decode($content);
30
+ $content = str_replace("<br />"," ",$content);
31
+ $content = str_replace("<p>"," ",$content);
32
+ $content = str_replace("</p>"," ",$content);
33
+
34
+ #line break
35
+ $content = str_replace("[br/]","<br/>",$content);
36
+ #other tags
37
+ $content = str_replace("\\[","&#91;",$content);
38
+ $content = str_replace("\\]","&#93",$content);
39
+ $content = str_replace("[","<",$content);
40
+ $content = str_replace("]",">",$content);
41
+ $content = str_replace("&lsb;",'[',$content);
42
+ $content = str_replace("&rsb;",']',$content);
43
+ ob_start();
44
+ eval ($content);
45
+ if($args['debug'] == 1){
46
+ $content =(htmlspecialchars($content,ENT_QUOTES));
47
+ echo ("<pre>".$content."</pre>");
48
+ }
49
+ $returned = ob_get_clean();
50
+ return $returned;
51
+
52
+ }
53
+
54
+ function allow_php_menu(){
55
+ add_submenu_page('options-general.php','Allow PHP in posts and pages', 'Allow PHP in posts', 'edit_posts', 'allow-php-admin', 'allow_php_options');
56
+ }
57
+ function allow_php_options(){
58
+ ?>
59
+ <h2>Allow PHP in posts and pages</h2>
60
+ <div style='float:right; display:inline; margin-left:25px; margin-bottom:10px; margin-right:15px; padding:5px; background-color:#ffffcc; border:1px solid #ddddaa;'>
61
+ <span style='font-size:1em; color:#999; display:block; line-height:1.2em;'>Developed by <a href='http://www.hitreach.co.uk' target="_blank" style='text-decoration:none;'>Hit Reach</a></span>
62
+ <span style='font-size:1em; color:#999; display:block; line-height:1.2em;'>Check out our other <a href='http://www.hitreach.co.uk/services/wordpress-plugins/' target="_blank" style='text-decoration:none;'>Wordpress Plugins</a></span>
63
+ <span style='font-size:1em; color:#999; display:block; line-height:1.2em;'>Version: 1.0 <a href='http://www.hitreach.co.uk/wordpress-plugins/allow-php-in-posts-and-pages/' target="_blank" style='text-decoration:none;'>Support &amp; Comments</a></span></div>
64
+
65
+ <p>Allow PHP in posts and pages adds the functionality to include PHP in wordpress posts and pages by adding a simple shortcode <span style='color:green'>[php]</span><em>.code.</em><span style='color:green'>[/php]</span></p>
66
+ <p>This plugin strips away the automatically generated wordpress &lt;p&gt; and &lt;br/&gt; tags but still allows the addition of your own &lt;p&gt; and &lt;br/&gt; tags
67
+ <h3>Usage</h3>
68
+ <p>To add the PHP code to your post or page simply place any PHP code inside the shortcode tags.<p>
69
+ <em>For example: </em>If you wanted to add content that is visible to a particular user id:<br/>
70
+ <pre>
71
+ [php]
72
+ global $user_ID;
73
+ if($user_ID == 1){
74
+ echo "Hello World";
75
+ }
76
+ [/php]
77
+ </pre>
78
+ <p>This code will output Hello World to only user id #1, and no one else</p>
79
+ <p>in addition, should this code not be working (for example a missing ";") simply just change the [php] to be [php debug=1]</p>
80
+ <pre>
81
+ [php debug=1]
82
+ global $user_ID;
83
+ if($user_ID == 1){
84
+ echo "Hello World"
85
+ }
86
+ [/php]
87
+ </pre>
88
+ <p>Will result in the output:
89
+ <pre>
90
+ Parse error: syntax error, unexpected '}', expecting ',' or ';' in XXX : eval()'d code on line 5
91
+ global $user_ID;
92
+ if($user_ID == 1){
93
+ echo "Hello World"
94
+ }
95
+ </pre></p>
96
+ <h3>Some Important Notes</h3>
97
+ This plugin strips away all instances of &lt;p&gt; and &lt;br /&gt; therefore code has been added so that if you wish to use tags in your output (e.g.):
98
+ <pre>
99
+ [php]
100
+ echo "hello &lt;br /&gt; world";
101
+ [/php]
102
+ </pre>
103
+ the &lt; and &gt; tags will need to be swapped for [ and ] respectively so &lt;p&gt; becomes [p] and &lt;/p&gt; becomes [/p] which is converted back to &lt;p&gt; at runtime. these [ ] work for all tags (p, strong, em etc.).
104
+ <pre>
105
+ [php]
106
+ echo "hello [br /] world";
107
+ [/php]
108
+ </pre>
109
+ <h3>Tag list</h3>
110
+ <table cellpadding="5" cellspacing="1" style='border:1px #ddd solid' width='60%'>
111
+
112
+ <tr>
113
+ <th align="left" style="padding:5px; background:#ffffcc">For</th>
114
+ <th align="left" style="padding:5px; background:#ffffcc">Write as</th>
115
+ </tr>
116
+ <tr>
117
+ <td align="left" style="padding:5px; background:#ffffcc">&lt;p&gt; ... &lt;/p&gt;</td>
118
+ <td align="left" style="padding:5px; background:#ffffcc">[p] ... [/p]</td>
119
+ </tr>
120
+ <tr>
121
+ <td align="left" style="padding:5px; background:#ffffcc">&lt;em&gt;...&lt;/em&gt;</td>
122
+ <td align="left" style="padding:5px; background:#ffffcc">[em]...[/em]</td>
123
+ </tr>
124
+ <tr>
125
+ <td align="left" style="padding:5px; background:#ffffcc">&lt;p style=''&gt; ... &lt;/p&gt;</td>
126
+ <td align="left" style="padding:5px; background:#ffffcc">[p style=''] ... [/p]</td>
127
+ </tr>
128
+ <tr>
129
+ <td align="left" style="padding:5px; background:#ffffcc">&lt;u&gt; ... &lt;/u&gt;</td>
130
+ <td align="left" style="padding:5px; background:#ffffcc">[u] ... [/u]</td>
131
+ </tr>
132
+ <tr>
133
+ <td align="left" style="padding:5px; background:#ffffcc">&lt;br /&gt;</td>
134
+ <td align="left" style="padding:5px; background:#ffffcc">[br /]</td>
135
+ </tr>
136
+
137
+ </table>
138
+ <?php
139
+ }
140
+ ?>