WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to add widgets to my wordpress site programmatically. I tried the following code from the codex docs:

class MyNewWidget extends WP_Widget {

    function MyNewWidget() {
        // Instantiate the parent object
        parent::__construct( false, 'My New Widget Title' );

    function widget( $args, $instance ) {
        // Widget output

    function update( $new_instance, $old_instance ) {
        // Save widget options

    function form( $instance ) {
        // Output admin widget options form

function myplugin_register_widgets() {
    register_widget( 'MyNewWidget' );

add_action( 'widgets_init', 'myplugin_register_widgets' );

But doesn't seem to work. I even tried the code from the question Programmatically add widgets to sidebars but to no avail. Please tell me if I am missing out something.


share|improve this question

here is an example that should work with the latest version of wordpress:

 class MyNewWidget extends WP_Widget {
        function MyNewWidget() {
            parent::WP_Widget(false, 'My New Widget');
        function form($instance) {
            // outputs the options form on admin
        function update($new_instance, $old_instance) {
            // processes widget options to be saved
            return $new_instance;
        function widget($args, $instance) {
            echo('<span>this is my widget</span>');
share|improve this answer

Your Answer


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.