自定义插件激活时,Widget部分上的Wordpress边栏不显示

时间:2017-05-26 07:04:28

标签: php wordpress widget

    Hello Team,

    I am using the latest version of WordPress(4.7.2). My custom plugin works smoothly up to 4.5.2. when I tried to install the plugin, upon activation, on the widget area section, sidebar section is not displayed. 

该插件在以前的Wordpress版本上运行顺畅。但现在它没有显示出来。请帮我找一个解决方案。

过去几天我坚持这个。如果你们帮助我找到最佳解决方案,将会很有帮助。 [请参阅管理区域窗口小部件显示] [1]

    Please see the following code:

    <?php
    /**
     * Add actions to widgets_init to load the widget.
     */
    add_action("widgets_init", "daily_quotes_load_widgets");
    add_action("wp_enqueue_scripts", "daily_quotes_enqueue_scripts");


    /*
    * fucntion to display contents in the webpage
    * @param null
    * @return display contents in a webpage
    */


    if (!function_exists('writeLog')) {

        /**
         * Function to add the plugin log to wordpress log file, added by BDT
         * @param object $log
         */
        function writeLog($log, $line = "",$file = "")  {

            if (WP_DEBUG === true) {

                $pluginLog = $log ." on line [" . $line . "] of [" . $file . "]\n";

                if ( is_array( $pluginLog ) || is_object( $pluginLog ) ) {
                    print_r( $pluginLog, true );
                } else {
                    error_log( $pluginLog );
                }

            }
        }

    }


    /**
     * function to register the widget "daily_quotes"
     * @author Test
     */
    function daily_quotes_load_widgets() {
        register_widget("Daily_Quotes");
    }

    /**
     * function to enqueue the styles and scripts used in the widget "daily_quotes"
     * @author Test
     */
    function daily_quotes_enqueue_scripts() {
        wp_enqueue_style("styles", plugin_dir_url(__FILE__) . "styles.css");
        wp_enqueue_script("scripts", plugin_dir_url(__FILE__) . "scripts.js", array("jquery"), "1.0.0", true);
    }

    /**
     * Daily_Quotes: Class which contains the functions for the display of the Daily Quote widget
     *
     * @author  Test
     */
    class Daily_Quotes extends WP_Widget {

        protected $plugin_slug;

        /**
         * Constructor
         */
        function Daily_Quotes() {
            include( plugin_dir_path(__FILE__) . 'class-quotery-quote.php' );
            $this->plugin_slug = Quotery_Quote::get_instance()->get_plugin_slug();
            $idBase = "daily_quotes_id";
            $name = "Daily Quotes";
            $description = "A widget that displays the daily inspirational quotes.";

            /* Widget settings. */
            $widgetOptions = array(
                "classname" => "daily_quotes", // CSS classname of the widget container
                "description" => $description // widget description which appears in admin area (Available widgets)
            );

            /* Widget control settings. */
            $controlOptions = array(
                "width" => 250, // width of the fully expanded control form in admin area (Sidebar) //modified the width ref : #103336
                "id_base" => $idBase // ID of the widget container. This is used for multi-widgets . Id of each instance will be like {$id_base}-{$unique_number}
            );

            /* Create the widget. */
            $this->WP_Widget($idBase, $name, $widgetOptions, $controlOptions);
        }

        /**
         * Function to display the daily_quotes widget on the screen.
         *
         * @param   Array   $args   array of arguments
         * @param   Object  $instance   widget instance
         * @author  Test
         */
        function widget($args, $instance) {
            global $post;

            /* Our variables from the widget settings. */
            $title = apply_filters("widget_title", $instance["title"]);


            echo $args["before_widget"];
            Quotery_Quote::get_instance()->quote_html($instance);
            ?>
            <?php
            echo $args["after_widget"];
        }

        /**
         * function to update the widget settings in admin area.
         * @param   Object  $newInstance    New instance of the widget
         * @param   Object  $oldInstance    Old instance of the widget
         * @return  Object  updated instance of the widget
         * @author  Test
         */
        function update($newInstance, $oldInstance) {
            $instance = $oldInstance;

            /* Strip tags for title and name to remove HTML. */
            $instance["title"] = strip_tags($newInstance["title"]);

            $instance["author"] = strip_tags($newInstance["author"]);

            $instance['topics'] = Quotery_Quote::get_instance()->filter_in_array($new_instance['topics'], Quotery_Quote::get_instance()->get_topics_options());

            /* Strip tags for message and name to remove HTML. */
            $instance["topics"] = strip_tags($newInstance["topics"]);

            /* Strip tags for message and name to remove HTML. */
            $instance["border"] = strip_tags($newInstance["border"]);

        writeLog("Updated the daily quote with the new admin settings", basename(__LINE__), basename(__FILE__));

            return $instance;


        }

        /**
         * Displays the widget settings on the widget panel in admin area.
         * @param   Object  Widget instance
         * @author  Test
         */
        function form($instance) {

            $instance = wp_parse_args(
                    (array) $instance, Quotery_Quote::get_instance()->get_quote_default_settings()
            );

            /* Default widget settings. */
            $defaults = array("title" => "");
            $defaults = array("topics" => "");
            $defaults = array("border" => "");
            $defaults = array("author" => "");
            $instance = wp_parse_args((array) $instance, $defaults);
            ?>

            <!-- Widget Title: Text Box -->
            <p>
                <label for="<?php echo $this->get_field_id("title"); ?>">
                    <?php _e("Title", "daily_quotes") . " : "; ?>
                </label>
                <input id="<?php echo $this->get_field_id("title"); ?>" name="<?php echo $this->get_field_name("title"); ?>" value="<?php echo $instance["title"]; ?>" />
            </p>

            <p>
                <label for="<?php echo $this->get_field_id('topics'); ?>"><?php _e('Topic:', $this->plugin_slug) ?></label>
                <select class="widefat" id="<?php echo $this->get_field_id('topics'); ?>" name="<?php echo $this->get_field_name('topics'); ?>">
                    <?php foreach (Quotery_Quote::get_instance()->get_topics_options() as $value => $name): ?>
                        <option value="<?php echo $value ?>"<?php echo $value == $instance['topics'] ? ' selected="selected"' : '' ?>><?php echo $name ?></option>
                    <?php endforeach ?>
                </select>
            </p>

            <!-- Widget Title: Text Box -->
            <p>
                <label for="<?php echo $this->get_field_id("border"); ?>">
                    <?php _e("Border Color", "daily_quotes") . " : "; ?>
                </label>
                <input id="<?php echo $this->get_field_id("border"); ?>" name="<?php echo $this->get_field_name("border"); ?>" value="<?php echo $instance["border"]; ?>" />
            </p>

            <?php
        }

    }
    ?>




  [1]: https://i.stack.imgur.com/iabkL.png

1 个答案:

答案 0 :(得分:0)


  

注意:自版本4.3.0起,Daily_Quotes中WP_Widget的被调用构造函数方法已弃用!请改用

__construct()
。在 D:\ xampp \ htdocs \ demowp \ wp-includes \ functions.php 3891

声明一个函数然后调用父构造函数解决了这个问题。

class Daily_Quotes extends WP_Widget {
function __construct(){
   parent::__construct(...) // calls constructor from WP_Widget class
}
}