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();
}
  • you must register the class of your widget register_widget("Level_system_Widgets");. – Kaperto Aug 11 '18 at 17:34
  • Thanks do I put it in my init function and replace the current register_widget? Or where do I put it? :) – slider2013 Aug 11 '18 at 17:44
  • i replaced: register_widget( 'My_Widget' ); With: register_widget("Level_system_Widgets"); Still not showing any widgets? – slider2013 Aug 11 '18 at 18:03
0

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

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

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

After that please activate 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.wpadami.com/
Description: Did you try this widget?
Version: 1
Author: Serkan Algur
Author URI: http://www.wpadami.com
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;
    }

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

Working Sample

This code works.

| improve this answer | |
  • 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? – slider2013 Aug 11 '18 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 – Serkan Algur Aug 11 '18 at 20:14
  • Please try new code from my answer – Serkan Algur Aug 11 '18 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 :) – slider2013 Aug 11 '18 at 20:26
  • If you don't create basic widgets, you can't see any widget :) – Serkan Algur Aug 11 '18 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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