WordPress.org

Ready to get started?Download WordPress

Plugin Directory

Allow PHP in Posts and Pages

Allow PHP in posts and pages allows you to add php functionality to WordPress Posts and Pages

Usage

Using the plugin couldn't be simpler; the plugin has 2 variants of shortcode available, [php] and [allowphp] other versions of the shortcode may exist but are not fully supported.

To add the PHP code to your post or page simply place any PHP code inside the shortcode tags as follows:

[php]
    global $user_ID;
    if($user_ID == 1){
        echo "Hello World";
    }
[/php]
This code will output Hello World to only user id #1, and no one else

The shortcode can take a some additional attributes to assist with PHP writing

  • "debug" - [1/0] - Turns on error reporting and display errors for snippets
  • "silentdebug" - [1/0] - When used with "debug" shows errors in the source code only
  • "mode" - ["old"/"new] - Maintained for backwards compatibility only - When set to "old" enables the pre version 2.2.0 code replacement system for that shortcode, when set to "new" with the preparse option enabled it uses the newer code replacement system for that shortcode

Code Snippets

In addition to these attributes, a new one has been added called "function". The function attribute allows the addition of user defined code snippets into the content. The shortcode for snippets is [php function=x] or [allowphp function=x]

To integrate the snippets with the advanced filter, the shortcode should not contain any additional attributes or spaces

Code snippets can be added in the plugin's options page and can be re-used on as many pages as you like

Advanced Filter

The advanced filter is the newest feature of the plugin.

It allows the user to write standard html directly into the html post editor without needing tag replacements.

There are 2 ways to enable the Advanced Filter, the easiest way is to enable it in the plugin options page, the other way is to add "useadvancedfilter"onto the standard shortcode to use it on a shortcode by shortcode basis:

[php useadvancedfilter]...[/php useadvancedfilter]

or [allowphp useadvancedfilter]...[/allowphp useadvancedfilter]

Variable Sharing

The plugin class contains an array with can be used to share variables around the page, between blocks of php code anywhere.

Variable delarations can be made by accessing the global variable $allow_php as follows:

[php]
    global $allow_php;
    $allow_php->_shared['my_key'] = 'my value';
[/php]
This code will set "my value" to index "my_key" within the shared array
[php]
    global $allow_php;
    echo $allow_php->_shared['my_key'];
[/php]
This code will output the index "my_key" within the shared array

This can be useful for passing post information to the sidebar :)

Some Important Notes

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.):

[php] 
    echo "hello &lt;br /&gt; world"; 
[/php]

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.).

[php] 
    echo "hello [br /] world"; 
[/php]

With code written pre-version 2.2.0 you may need to turn on support for the older replacement method as you may experience issues with the code being disrupted as the replacement method has been changed, however you can set the tag replacement method to be the older version in the plugin options or by adding mode=old to the [php] tag, similarly, if you wish to use the new method when you have the old method turn on in the plugin options, just add mode=new to the [php] tag. In the current release, only known html tags are altered by the code, other tags and square bracket items (such as PHP arrays) are no longer affected.

The code replacement method isn't always perfect however, as certain items such as arrays cannot be accessed during echos and will need to be set as a variable before echo (this doesn't apply to the advanced filter)

echo "[p]".$myArray[0]."[/p]"
as
    $mya = $myArray[0];
    echo "[p]".$mya."[/p];

Requires: 2.5 or higher
Compatible up to: 3.5.2
Last Updated: 2013-4-30
Downloads: 158,864

Ratings

4 stars
4.3 out of 5 stars

Support

0 of 7 support threads in the last two months have been resolved.

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

100,1,1 100,1,1
100,3,3
100,2,2
100,1,1
100,1,1
100,1,1
0,5,0 100,1,1 100,1,1 100,1,1 100,2,2
100,2,2 0,2,0
55,11,6
33,3,1
0,1,0 0,1,0
100,2,2 100,1,1
100,2,2 0,1,0
100,7,7 100,2,2
100,1,1
0,1,0
100,4,4
25,4,1
100,1,1