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

1 Answer 1


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.

  • 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, 2011 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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