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' );
This code works. Tested with latest WordPress version (v5.8.1 as 26.10.2021)