2

I am trying to create a simple plugin widget for wordpress, like this

<?php

// The widget class
class My_Custom_Widget extends WP_Widget {

    // Main constructor
    public function __construct() {
        parent::__construct(
            'my_custom_widget',
            __( 'My Custom Widget', 'text_domain' ),
            array(
                'customize_selective_refresh' => true,
            )
        );
    }

    // The widget form (for the backend )
    public function form( $instance) {}

    // Update widget settings
    public function update($new_instance, $old_instance) {}

    public function helloWorld(){
        echo 'Hello World';
    }

    // Display the widget
    public function widget( $args, $instance ) {
        helloWorld();
    }
}

// Register the widget
function my_register_custom_widget() {
    register_widget( 'My_Custom_Widget' );
}
add_action( 'widgets_init', 'my_register_custom_widget' );

You will see that inside function widget i have call to function helloWorld(), but i got error when displaying widget like this

Fatal error: Call to undefined function helloWorld() in /wp-content/plugins/my-widget-plugin/my-widget-plugin.php on line 38

Why I can not call function inside function?

1
  • Which helloWorld() it'll call? Commented Jun 8, 2020 at 10:20

1 Answer 1

2

You forget add $this:

    // Display the widget
    public function widget( $args, $instance ) {
        $this->helloWorld();
    }

Hope help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.