I am just creating a plugin in wordpress. In this plugin I am trying to create a widget, In that widget it will show a title, input text and text area in the widget section and when the values will be entered it will show to the site frontend. For that my code is like this

Plugin Name: WP Simple Plugin
Plugin URI: http://www.wordpress.org
Description: A Plugin to show simple text in widget
Version: 0.1

class wp_my_plugin extends WP_Widget {
// constructor
    function wp_my_plugin() {
        parent::WP_Widget(false, $name = __('My Widget', 'wp_widget_plugin') );

   function form($instance) {
    if($instance) {
      $title = esc_attr($instance['title']);
      $text = esc_attr($instance['text']);
      $textarea = esc_attr($instance['textarea']);
    } else {
      $title = '';
      $text = '';
      $textarea = '';
      <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title', 'wp_widget_plugin'); ?></label>
      <input class="widefat" type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title');?>" value="<?php echo $title; ?>"/>
      <label for="<?php echo $this->get_field_id('text')?>"><?php _e('Text', 'wp_widget_plugin');?></label>
      <input type="text" class="widefat" name="<?php echo $this->get_field_name('text');?>" id="<?php echo get_field_id('text');?>" value="<?php echo $text; ?>" />
      <label for="<?php echo $this->get_field_id('textarea');?>"><?php _e('Textarea','wp_widget_plugin'); ?></label>
      <textarea name="<?php echo $this->get_field_name('textarea'); ?>" id="<?php echo $this->get_field_id('textarea'); ?>" class="widefat"  /><?php echo $textarea; ?></textarea>


  function update($new_instance, $old_instance) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['text'] = strip_tags($new_instance['text']);
    $instance['textarea'] = strip_tags($new_instance['textarea']);
    return $instance;
    /* */

  function widget($args, $instance) {
    extract ($args);
    //The widget Options
    $title = apply_filters('widget_title' , $instance['title']);
    $text = $instance['text'];
    $textarea = $instance['textarea'];
    echo $before_widget;
    //Display Widget
    echo '<div class="widget-text wp_widget_plugin_box">';

    //Check If Title Is Set
    if($title) {
      echo $before_title.$title.$after_title;

    //check if text is set
    if($text) {
      echo '<p class="wp_widget_plugin_text">'.$text.'</p>';

    //check if texrarea is set
    if($textarea) {
      echo '<p class="wp_widget_plugin_textarea">'.$textarea.'</p>';
    echo '</div>';
    echo $after_widget;

// register widget
add_action('widgets_init', 'wp_my_plugin');


But when I am trying activate the plugin from dashboard it is showing error like

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'wp_my_plugin' not found or invalid function name in /wordpress/wp-includes/plugin.php on line 406 

and the plugin is not activating So can someone kindly tell me how to solve this. Any help will be really appreciable. Thanks

1 Answer 1


The second parameter for add_action() must be a function or a member of an object. In the second case you have to pass an array ( $object, 'method_name').

To register the widget from within the class, add the following method to your widget class:

 * Tell WP we want to use this widget.
 * @wp-hook widgets_init
 * @return void
public static function register()
    register_widget( __CLASS__ );

Then change your add_action() call to:

add_action( 'widgets_init', array ( 'wp_my_plugin', 'register' ) );

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.