1

This is my first WordPress plugin ever, so i'm a little noob. But why is my widget not showing within all the other widgets in my WP-Admin interface? It should be visible when i activate the plugin. But it's not. Any help appreciated!

includes/class-level_system-widgets.php

class Level_system_Widgets extends WP_Widget {

    public function __construct(){
        $widget_ops = array( 
            'classname' => 'my_widget',
            'description' => 'My Widget is awesome',
        );
        parent::__construct( 'my_widget', 'My Widget', $widget_ops );
    }

    public function widget( $args, $instance ) {
        // outputs the content of the widget
    }   

    public function form( $instance ) {
        // outputs the options form on admin
    }

    public function update( $new_instance, $old_instance ) {
        // processes widget options to be saved
    }

    public function init() {
        add_action( 'widgets_init', function(){
            register_widget( 'My_Widget' );
        }); 
    }
}

level_system.php

function activate_level_system() {
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-level_system-widgets.php';
    Level_system_Widgets::init();
}
3
  • you must register the class of your widget register_widget("Level_system_Widgets");.
    – Kaperto
    Aug 11, 2018 at 17:34
  • Thanks do I put it in my init function and replace the current register_widget? Or where do I put it? :)
    – BitSubmit
    Aug 11, 2018 at 17:44
  • i replaced: register_widget( 'My_Widget' ); With: register_widget("Level_system_Widgets"); Still not showing any widgets?
    – BitSubmit
    Aug 11, 2018 at 18:03

1 Answer 1

0

Updated my answer based on your comments. First of all;

Please read how to create a plugin: https://codex.wordpress.org/Writing_a_Plugin

Secondly, create a folder and your file in the wp-content/plugins directory. Add this sample plugin code below.

After that please activate the plugin from Dashboard.

old answer

When you register widget as a plugin you need to use add_action after your class. widgets init loads before your class.

add_action('widgets_init', create_function('', 'return register_widget("Level_system_Widgets");'));

Add this code after your class (Code Updated with sample WordPress Widgets API code);

New Answer

<?php
/*
Plugin Name: Did you try this widget?
Plugin URI: http://www.serkanalgur.com.tr/
Description: Did you try this widget?
Version: 1
Author: Serkan Algur
Author URI: http://www.serkanalgur.com.tr
License: GPL2
*/

class Level_system_Widgets extends WP_Widget {

    public function __construct() {
        $widget_ops = array(
            'classname'   => 'level_system_widget',
            'description' => 'Level System Widget',
        );
        parent::__construct( 'level_system_widget', 'Level System Widget', $widget_ops );
    }

    public function widget( $args, $instance ) {
        echo $args['before_widget'];
        if ( ! empty( $instance['title'] ) ) {
            echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
        }
        echo esc_html__( 'Hello, World!', 'text_domain' );
        echo $args['after_widget'];
    }

    public function form( $instance ) {
        $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
        ?>
    <p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label> 
    <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
    </p>
        <?php
    }

    public function update( $new_instance, $old_instance ) {
        $instance          = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';

        return $instance;
    }

}

function register_widgets() {
    return register_widget( 'Level_system_Widgets' );
}
add_action( 'widgets_init', 'register_widgets' );

Working Sample

This code works. Tested with latest WordPress version (v5.8.1 as 26.10.2021)

10
  • Thanks. I tried your code but still no widget appears. Do I just call the class itself from level_system.php instead of calling the function init within the level_system class? I am testing the wp-admin interface on localhost by editing the php in the plugin editor. can this be a problem? Or it doesn't matter?
    – BitSubmit
    Aug 11, 2018 at 20:01
  • You dont use this code with plugin right, you create actual widget codes with your plugin? I will edit my sample code with a simple widget. Gimme 2 min Aug 11, 2018 at 20:14
  • Please try new code from my answer Aug 11, 2018 at 20:20
  • I will try the new code thanks. I need to get the basic functionality to work before I add the actual widget code :)
    – BitSubmit
    Aug 11, 2018 at 20:26
  • 1
    You're the champ (Y)
    – BitSubmit
    Aug 11, 2018 at 20:54

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.