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 have created a widget that have to run some code using the wp_footer hook as well. But if you extend the WP_Widget, the wp_footer runs twice! Has someone already past for the same issue?

Here is an example:

/*
Plugin Name: Test
Description: Test Plugin by Romulo De Lazzari
Version: 1.0
Author: Romulo De Lazzari
*/

new Test();

class Test extends WP_Widget{

    public function __construct(){

        parent::__construct('test-widget', 'Test Widget');      

        add_action('widgets_init', array($this, 'register_my_widget'));
        add_action('wp_footer', array($this, 'do_something'));
    }

    public function register_my_widget(){
        return register_widget("Test");
    }

    public function do_something(){
        echo "<p>Hello! This is a test!</p>";
    }

    public function widget($args, $instance) {

    }

    public function form($instance){

    }

    public function update($new_instance, $old_instance){

    }

}

The method do_something() is running twice here, if I take off the "Widget part" it runs well. Could someone help me?

Thanks in advance, Romulo

share|improve this question

1 Answer 1

up vote 1 down vote accepted

That code doesn't look like it conforms to the current Widgets API. Perhaps your problem is related?

First potential issue: your class test should be plugin-slug-test, in order to avoid naming conflicts.

Second potential issue: the only functions inside of your WP_Widget extending class should be:

  • function plugin-slug-test() {}
  • function widget( $args, $instance ) {}
  • function update( $new_instance, $old_instance ) {}
  • function form( $instance ) {}

You need to move your add_action() calls outside of your Widget class.

Third potential issue: why do you declare all of your Widget class functions to be public?

Fourth potential issue: I don't think you need to declare new Test(); for a Widget class.

share|improve this answer
    
Thanks, actually isn't the name conviction the problem, but the second tip have made me realise that I have to create two classes in the same file. One for the Widget (only with the widget, form and update methods) and another one for the wp_footer hook. –  romulodl Jul 4 '11 at 15:24

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.