Warum AJAX? Ganz einfach weil wenn ich auf reines Javascript setzen würde ergeben sich folgende Nachteile:
- Die Uhrzeit die angezeigt wird ist abhängig von der eingestellten Uhrzeit des Users. Nicht zuverlässig.
- Wenn der User kein Javascript eingestellt hat dann wird einfach gar nichts angezeigt. Mit AJAX passiert dies nicht, die Uhrzeit wird einfach nur nicht aktualisiert.
- Es sind nicht immer 6 Stunden Differenz zwischen New York und Berlin.
Ich habe jetzt probiert es selbst umzusetzen. Leider funktioniert es nicht ganz. Wenn ich bei den Optionen was eintrage dann ist beim nächsten Aufruf der Optionen der Eintrag wieder verschwunden.
Vielleicht sieht ja jemand den Fehler:
<?php
/*
Plugin Name: Timezones
Description: Anzeige zweierlei Timezones
Author: Moritz
Version: 1.0
*/
function widget_timezones_init() {
// Check for the required plugin functions. This will prevent fatal
// errors occurring when you deactivate the dynamic-sidebar plugin.
if ( !function_exists('register_sidebar_widget') )
return;
// This is the function that outputs our little Google search form.
function widget_timezones($args) {
global $post;
// $args is an array of strings that help widgets to conform to
// the active theme: before_widget, before_title, after_widget,
// and after_title are the array keys. Default tags: li and h2.
extract($args);
// Each widget can store its own options. We keep strings here.
$options = get_option('widget_timezones');
$timezone1 = $options['timezone1'];
$timezone2 = $options['timezone2'];
$title = $options['title'];
echo $before_widget;
if(!empty($title)) {
echo $before_title;
echo $title;
echo $after_title;
}
print_r($options);
echo $after_widget;
}
// This is the function that outputs the form to let the users edit
// the widget's title. It's an optional feature that users cry for.
function widget_timezones_control() {
// Get our options and see if we're handling a form submission.
$options = get_option('widget_timezones');
if ( !is_array($options) )
$options = array('title'=>'', 'timezone1'=>'', 'timezone2'=>'');
if ( $_POST['timezones-submit'] ) {
// Remember to sanitize and format use input appropriately.
$options['title'] = strip_tags(stripslashes($_POST['skypewidget-title']));
$options['timezone1'] = strip_tags(stripslashes($_POST['skypewidget-timezone1']));
$options['timezone2'] = strip_tags(stripslashes($_POST['skypewidget-timezone2']));
update_option('widget_timezones', $options);
}
// Be sure you format your options to be valid HTML attributes.
$title = htmlspecialchars($options['title'], ENT_QUOTES);
$timezone1 = htmlspecialchars($options['timezone1'], ENT_QUOTES);
$timezone2 = htmlspecialchars($options['timezone2'], ENT_QUOTES);
// Here is our little form segment. Notice that we don't need a
// complete form. This will be embedded into the existing form.
echo '<p><label for="timezones-title">' . __('Title:') . ' <input style="width: 200px;" id="timezones-title" name="timezones-title" type="text" value="'.$title.'" /></label></p>';
echo '<p><label for="timezones-timezone1">' . __('Zeitzone 1:') . ' <input style="width: 200px;" id="timezones-timezone1" name="timezones-timezone1" type="text" value="'.$timezone1.'" /></label></p>';
echo '<p><label for="timezones-timezone2">' . __('Zeitzone 2:') . ' <input style="width: 200px;" id="timezones-timezone2" name="timezones-timezone2" type="text" value="'.$timezone2.'" /></label></p>';
}
// This registers our widget so it appears with the other available
// widgets and can be dragged and dropped into any active sidebars.
register_sidebar_widget(array('Timezones', 'widgets'), 'widget_timezones');
// This registers our optional widget control form. Because of this
// our widget will have a button that reveals a 300x100 pixel form.
register_widget_control(array('Timezones', 'widgets'), 'widget_timezones_control');
}
// Run our code later in case this loads prior to any required plugins.
add_action('widgets_init', 'widget_timezones_init');
?>
Alles anzeigen