繁体   English   中英

WordPress插件开发期间出错,PHP提取功能

[英]Error During WordPress plugin Development, PHP extract function

我是WordPress插件开发的新手。 我有以下代码的插件形式方法。 当我在标题字段中输入任何内容并保存时,它给出警告:extract()期望参数1为数组,/ Applications / XAMPP / xamppfiles / htdocs / NGO / wp-content / plugins / messagner中给出的null

 public function form($instance)
        {
                extract($instance);
                print_r($instance);
                ?>
                <p>
                        <label for="<?php echo $this->get_field_id('title');?> ">Title</label>
                        <input 
                        class="widefat"
                        id = "<?php echo $this->get_field_id('title');?>"
                        name = "<?php echo $this->get_field_name('title');?>"
                        value = "<?php if(isset($title)) echo esc_attr($title); ?>"
                        />
                </p>
                <p>
                        <label for="<?php echo $this->get_field_id('description'); ?>"></label>
                        <textarea 
                        class = "widefat"
                        rows = 10
                        id = "<?php echo $this->get_field_id('description');?>"
                        name = "<?php echo $this->get_field_name('description'); ?>"
                        value = "<?php if(isset($description)) echo esc_attr($description); ?>"
                        >
                </textarea>
                       </p>
                <?php

        }?>

我不知道我在哪里犯了一些愚蠢的错误,请帮忙。 谢谢。

请仔细阅读此代码,然后,在这种情况下,$ instance返回为空。 它应该得到array。

 <?php
    /**
     * Example Widget Class
     */
    class example_widget extends WP_Widget {


        /** constructor -- name this the same as the class above */
        function example_widget() {
            parent::WP_Widget(false, $name = 'Example Text Widget');    
        }

        /** @see WP_Widget::widget -- do not rename this */
        function widget($args, $instance) { 
            extract( $args );
            $title      = apply_filters('widget_title', $instance['title']);
            $message    = $instance['message'];
            ?>
                  <?php echo $before_widget; ?>
                      <?php if ( $title )
                            echo $before_title . $title . $after_title; ?>
                                <ul>
                                    <li><?php echo $message; ?></li>
                                </ul>
                  <?php echo $after_widget; ?>
            <?php
        }

        /** @see WP_Widget::update -- do not rename this */
        function update($new_instance, $old_instance) {     
            $instance = $old_instance;
            $instance['title'] = strip_tags($new_instance['title']);
            $instance['message'] = strip_tags($new_instance['message']);
            return $instance;
        }

        /** @see WP_Widget::form -- do not rename this */
        function form($instance) {  

            $title      = esc_attr($instance['title']);
            $message    = esc_attr($instance['message']);
            ?>
             <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('message'); ?>"><?php _e('Simple Message'); ?></label> 
              <input class="widefat" id="<?php echo $this->get_field_id('message'); ?>" name="<?php echo $this->get_field_name('message'); ?>" type="text" value="<?php echo $message; ?>" />
            </p>
            <?php 
        }


    } // end class example_widget
    add_action('widgets_init', create_function('', 'return register_widget("example_widget");'));
    ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM