Transform php code into a widget?

Here is a stand-alone answer. Building a widget to echo hard-coded PHP is trivial.

class PHP_Widget_wpse_80256 extends WP_Widget {
  function __construct() {
    $opts = array(
      'description' => 'Display Some Hard Coded PHP content'
    );
    parent::WP_Widget(
      'my-hc-php-content',
      'Some PHP',
      $opts
    );
  }
  function widget($args,$instance) {
    // PHP goes here, like this:
    echo 'PHP generated content';
  }
}
function register_my_widgets() {
  register_widget('PHP_Widget_wpse_80256');
}
add_action('widgets_init','register_my_widgets');

There is no need for the overhead of a plugin or the overhead of evaling your widget text, which is what the plugin mentioned does.

Leave a Comment