admin管理员组文章数量:1277267
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();
}
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();
}
Share
Improve this question
asked Aug 11, 2018 at 17:27
BitSubmitBitSubmit
452 silver badges6 bronze badges
3
|
1 Answer
Reset to default 0Updated my answer based on your comments. First of all;
Please read how to create a plugin: https://codex.wordpress/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.tr/
Description: Did you try this widget?
Version: 1
Author: Serkan Algur
Author URI: http://www.serkanalgur.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)
本文标签: phpHow to make my custom widget appear within WordPress widgets Plugin development
版权声明:本文标题:php - How to make my custom widget appear within WordPress widgets? Plugin development 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741247255a2365117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
register_widget("Level_system_Widgets");
. – Kaperto Commented Aug 11, 2018 at 17:34