PHP Shortcode Wordpress Plugin - Rating, Reviews, Demo & Download

PHP Shortcode Wordpress Plugin - Rating, Reviews, Demo & Download
No ratings yet
Free
Follow for free plugins, new theme releases and theme news

Plugin Description

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

Screenshots

No screenshots provided


Reviews & Comments