我正在尝试我的第一个项目自定义插件,它似乎约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");') );
?>
答案 0 :(得分:0)
在form()
函数的第3行,您定义变量$url
,但在变量替换中将其称为$event_url
。要与脚本的其余部分保持一致,请将form()
中的第3行更改为...
$event_url = $instance['event_url'];