激活的插件未以后端形式显示

时间:2016-09-26 07:26:02

标签: php wordpress

我是一个wordpress新手,我制作了一个简单的小部件插件,显示标题和文字。一切似乎都很好,插件已激活,但小部件不会显示在后端小部件列表中。我做错了什么?

我已经搜索,检查并重新检查了高低,但似乎无法确定错误。

这是我的代码文件:

的functions.php

<?php
    function ms_init_widgets($id){
       register_sidebar(array(
            'name' => 'Showcase',
            'id' => 'showcase',
            'before_widget'  => '',
            'after_widget' => '',
            'before_title' => '',
            'after_title' => ''
        ));
    }

add_action('widgets_init', 'ms_init_widgets');

新-陈列柜-widget.php

<?php
    /*
    *Plugin name:   New Showcase Widget
    *Description: A new showcase widget that displays a title and text
    *Author: Shefalizzle
    *version: 1.0
    *
    */

    include('class.new-showcase-widget.php');

    function register_new_showcase_widget(){
        register_widget('New_Showcase_Widget');
    }

    add_action('widgets-init', 'register_new_showcase_widget');

class.new-陈列柜-widget.php

<?php

class New_Showcase_Widget extends WP_Widget {

    // constructor
    public function __construct() {
        $widget_ops = array( 
            'classname' => 'new_showcase_widget',
            'description' => __('A new widget I created that is actually working', 'text_domain'),
        );
        parent::__construct( 'new_showcase_widget', 'New_Showcase_Widget', $widget_ops );
    }

    // widget form creation
    function form($instance) {  
    /* ... */
    if( $instance) {
     $title = esc_attr($instance['title']);
     $heading = esc_attr($instance['heading']);
     $text = esc_attr($instance['text']);
    } else {
        $title = '';
        $heading = '';
        $text = '';
    }
    ?>

    <p>
    <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
    </p>

    <p>
    <label for="<?php echo $this->get_field_id('heading'); ?>"><?php _e('Heading:'); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('heading'); ?>" name="<?php echo $this->get_field_name('heading'); ?>" type="text" value="<?php echo $heading; ?>" />
    </p>

    <p>
    <label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Text:'); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></input>
    </p>

    <?php

    }

    // widget update
    function update($new_instance, $old_instance) {
        /* ... */
        $instance = $old_instance;

        $instance['title'] = strip_tags($new_instance['title']);
        $instance['text'] = strip_tags($new_instance['text']);
        $instance['textarea'] = strip_tags($new_instance['textarea']);

        return $instance;
    }

    // widget display
    function widget($args, $instance) {
        /* ... */
        extract($args);

        $title = apply_filters('widget_title', $instance['title']);
        $heading = $instance['heading'];
        $text = $instance['text'];

        echo $before_widget;

         // Check if title is set
        if ( $title ) {
          echo $before_title . $title . $after_title;
        }

        if($heading) {
            echo '<h1>'.$heading.'</h1>';
        }

        // Check if text is set
        if( $text ) {
            echo '<p>'.$text.'</p>';
        }

        echo $after_widget;     
    }   
}

我认为表单功能有问题,但我似乎无法弄清楚

1 个答案:

答案 0 :(得分:0)

您是否尝试在index.php页面或所需页面中显示Widget?

<?php if(is_active_sidebar('showcase')) : ?>
     <?php dynamic_sidebar('showcase'); ?>
<?php endif; ?>