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.

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.

Thankyou

share|improve this question

1 Answer 1

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>');
        }
    }
    register_widget('MyNewWidget');
share|improve this answer

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.