WordPress.org

Ready to get started?Download WordPress

Plugin Directory

!This plugin hasn't been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.

PHP Shortcode

Based on kukukuan's Inline PHP plugin, this plugin allows you to embed and run PHP code in posts, pages or widgets with a WordPress shortcode.

Based on kukukuan's Inline PHP, this plugin allows you to embed and run PHP code in posts, pages or widgets* with a WordPress shortcode.

(* Requires a shortcode enabled widget plugin, such as Section Widget.)

Usage

The plugin provides two pairs of shortcodes - [php]code[/php] and [echo]code[/echo]. these two pairs of shortcodes resembles the functionality of the <?php code ?> and <?= code ?> tags in a normal PHP script, respectively.

For example:

The answer to the <em>ultimate</em> math challenge, <strong>1+2</strong>, is...
[php]
  $a = 1;
  $b = 2;

  echo $a + $b;
[/php]

Will become:

The answer to the <em>ultimate</em> math challenge, <strong>1+2</strong>, is...
3

The [echo] tag will automatically print the returned value of the first expression. Therefore, [echo]some_function()[/echo] is essentially equivalent to [php]echo some_function()[/php].

Some Important Notes

** This plugin will change the priority of the do_shortcode filter. If you are experiencing any conflict with other shortcode plugins, please disable the plugin and report the problem in the forums. **

Although I said the shortcode pairs resembles a <?php code ?> tag pair, there is an important difference. The PHP code in the shortcodes are executed in a "throw-away" local namespace, instead of the global one. All variables defined in a [php] code [/php] block cannot be accessed outside the block. Therefore, this will not work:

[php]
  $a = 1;
  $b = 2;
[/php]

The answer to the <em>ultimate</em> math challenge, <strong>1+2</strong>, is... [echo]$a+$b[/echo]

And neither would this:

[php]
  $my_array = array('apple','orange');

  foreach($my_array as $fruit):
[/php]

I like [echo]$fruit[/echo]

[php]endforeach;[/php]

To work around the first problem, you may use the global keyword:

[php]
  global $a, $b;
  $a = 1;
  $b = 2;
[/php]

The answer to the <em>ultimate</em> math challenge, <strong>1+2</strong>, is... [php]global $a, $b; echo $a+$b[/php]

And to work around the second problem, you may use "real" PHP closing tags within your [php] code [/php] block to switch between PHP and HTML mode:

[php]
  $my_array = array('apple','orange');

  foreach($my_array as $fruit):
?>

I like <?php echo $fruit; ?>

<?php
  endforeach;
[/php]

(Yes, it is a bit weird... you'd probably want to avoid doing that if possible.)

Tags: , , , ,

Requires: 2.5 or higher
Compatible up to: 2.8.3
Last Updated: 2009-8-4
Downloads: 6,026

Ratings

3 stars
3.4 out of 5 stars

Support

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
50,2,1
0,1,0
100,1,1
100,1,1
100,1,1