如何让我的定制小工具出现在WordPress小工具中?插件开发

时间:2018-08-11 作者:slider2013

这是我的第一个WordPress插件,所以我有点笨。但是为什么我的widget没有显示在WP管理界面中的所有其他widget中?当我激活插件时,它应该是可见的。但事实并非如此。感谢您的帮助!

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();
}

1 个回复
最合适的回答,由SO网友:Serkan Algur 整理而成

根据您的评论更新了我的答案。首先

请阅读如何创建插件:https://codex.wordpress.org/Writing_a_Plugin

其次,在wp-content/plugins 目录在下面添加此示例插件代码。

之后,请从仪表板激活插件。

旧答案

当您将小部件注册为插件时,您需要使用add_action下课后。widgets init在类之前加载。

add_action(\'widgets_init\', create_function(\'\', \'return register_widget("Level_system_Widgets");\'));
在类后添加此代码(使用示例WordPressWidgets API代码更新代码);

新答案

<?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

此代码works. 使用最新WordPress版本(v5.8.1 as 26.10.2021)进行测试

结束

相关推荐

Wp-login.php输入密码什么都不会发生

我有一个prettypeople网站。无论何时我尝试以管理员身份登录,无论是在/wp admin还是/wp登录并输入正确的凭据,都不会发生任何事情!页面刷新,如果我检查日志,我找不到任何东西!我能做什么?链接更改自prettypeople.nl/wp-login 收件人:https://prettypeople.nl/wp-login.php?redirect_to=https%3A%2F%2Fprettypeople.nl%2Fprettypeople%2F%2Fwp-admin%2F&reau