Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

how can I create a widget out of a php code? It's just a disqus code that I would like to place in the sidebar. I could just paste the code in the widget section (my tag), but then I would not be able to change it's order with the other wigets. This is why I want to convert this code into a widget.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

Use this plugin, PHP Code Widget. It allows php code inside widgets

The normal Text widget allows you to insert arbitrary Text and/or HTML code. This allows that too, but also parses any inserted PHP code and executes it. This makes it easier to migrate to a widget-based theme.

share|improve this answer
    
Haha, thank you! –  Maxwell Jan 12 '13 at 4:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.