0

我正在为一个项目尝试我的第一个自定义插件,它似乎大约 90% 工作(不知何故)。我可以保存标题,但不能保存第二个变量 (event_url) 的 URL 以保存或工作。

我为模板使用了一个示例插件,但我看到的所有示例都只有 1 个变量要保存。有人可以看看我的代码并告诉我为什么 event_url 不起作用。

<?php

/*
    Plugin Name: College of Engineering Events Widget
    Plugin URI: http://creative.umich.edu
    Description: Plugin created to display the events from the college of engineering sites
    Version: 1.0
    Author: Michigan Creative, Wally Kolcz
    Author URI: http://creative.umich.edu
    License: GPL2
*/

class CoE_Events extends WP_Widget {

    function CoE_Events(){
        $widget_ops = array('classname' => 'CoE_Events', 'description' => 'Plugin created to display the events from the college of engineering sites' );
        $this->WP_Widget('CoE_Events', 'CoE Events', $widget_ops);
    }

    function form($instance){
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'event_url'=>'' ) );
        $title = $instance['title'];
        $url = $instance['event_url'];
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>

        <p><label for="<?php echo $this->get_field_id('event_url'); ?>">Events Feed URL: <input class="widefat" id="<?php echo $this->get_field_id('event_url'); ?>" name="<?php echo $this->get_field_name('event_url'); ?>" type="text" value="<?php echo attribute_escape($event_url); ?>" /></label></p>

    <?php
      }

      function update($new_instance, $old_instance){
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['event_url'] = $new_instance['event_url'];
        return $instance;
      }

      function widget($args, $instance){
        extract($args, EXTR_SKIP);

        echo $before_widget;
        $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
        $event_url = empty($instance['event_url']) ? ' ' : apply_filters('widget_event_url', $instance['event_url']);

        if (empty($title)){
            $title = 'Events';
        }

        if (empty($event_url)){
            $event_url = 'http://www.engin.umich.edu/college/about/cal/event-calendar';
        }

        $json = event_url.'/futurejson';

        //Get the feed from the url the user provided
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $json);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
     $feeds = curl_exec($ch);
     curl_close($ch);

    //decode the data for php
    $data = json_decode($feeds,true);

    //var_dump($data);

    //Check to see if there are any errors in the feed. If yes, output error as result, if not, then start the process of creating block
    if (json_last_error() === JSON_ERROR_NONE) {
        //Is the json an array?
        if (is_array($data)) {
            //Start the CSS wrapper
            $events = '<div class="promo promoHolderEvents"><a class="promoEvents" href="'.$event_url.'"><h3>'.$title.'</h3><h4>'.getDatesHeaders($data['events'][0]['month'], $data['events'][1]['month']).'</h4>';
            //Loop over results to get the information and create the posts
            for ($i = 0; $i < 2; $i++) {
                $events.='<div class="pDateItem"><p class="pDate">'.addOrdinalNumberSuffix($data['events'][$i]['day']).'</p><p class="pDate_Info">'.$data['events'][$i]['title'].'</p></div>';
            }

            //End the wrapper'.$url.'
            $events.='<span class="linkCtA">see all</span></a></div>';
        }
    } else {
         switch (json_last_error()) {
            case JSON_ERROR_DEPTH:
                echo ' - Maximum stack depth exceeded';
            break;
            case JSON_ERROR_STATE_MISMATCH:
                echo ' - Underflow or the modes mismatch';
            break;
            case JSON_ERROR_CTRL_CHAR:
                echo ' - Unexpected control character found';
            break;
            case JSON_ERROR_SYNTAX:
                echo ' - Syntax error, malformed JSON';
            break;
            case JSON_ERROR_UTF8:
                echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
            break;
            default:
                echo ' - Unknown error';
            break;
        };
    } 

    echo $events;

    echo $after_widget;

    }

    function getDatesHeaders($m1, $m2){
        if($m1 != $m2){
            $months = date('M', strtotime('2013-'.$m1.'-01')).'/'.date('M', strtotime($m2.'-01-2013'));
        }else{
            $months = date('M', strtotime('2013-'.$m1.'-01'));
        }

        return $months;
    }

    function addOrdinalNumberSuffix($num) {
        if (!in_array(($num % 100),array(11,12,13))){
            switch ($num % 10) {
                // Handle 1st, 2nd, 3rd
                case 1:  return $num.'st';
                case 2:  return $num.'nd';
                case 3:  return $num.'rd';
            }
        }
        return $num.'th';
    }
}
add_action( 'widgets_init', create_function('', 'return register_widget("CoE_Events");') );
?>
4

1 回答 1

0

form()函数的第 3 行定义一个变量,然后在变量替换中$url引用它。$event_url为了与脚本的其余部分保持一致,请将第 3 行更改form()为...

$event_url = $instance['event_url']; 
于 2013-05-20T12:10:40.633 回答