Hallo Community,
so wie es aussieht brauche ich nochmal eure Hilfe.
Ich möchte ein Widget erstellen, welches zum Generieren einer Tabelle dient. Dabei soll es flexibel sein, wie viele Spalten und Zeilen diese hat. Das generieren der Input Felder im Backend funktioniert einwandfrei. Allerdings, so scheint es, schaffe ich es nicht, die Feldinhalte zu speichern, geschweige denn auszugeben. Übersehe ich da etwas? Oder kann es so gar nicht funktionieren?
Vielen Dank für Eure Mühen und Hilfe!
Sparkley
PHP
class table_widget extends WP_Widget { function __construct() { $widget_ops = array( 'classname' => 'table_det_widget', 'description' => 'Tabelle erstellen', ); parent:: __construct(false, $name = __('Tabelle'), $widget_ops); } function form($instance) { /*Ausgabe im Backend*/ echo '<label for="' . $this->get_field_id('wp_table_title') . '">Überschrift: </label>'; echo '<input class="widefat" type="text" value="' . $instance['wp_table_title'] .'" name="' . $this->get_field_name('wp_table_title') . '" id="' . $this->get_field_id('wp_table_title') . '" >'; echo '<label for="' . $this->get_field_id('wp_table_subtitle') . '">Unterüberschrift: </label>'; echo '<input class="widefat" type="text" value="' . $instance['wp_table_subtitle'] .'" name="' . $this->get_field_name('wp_table_subtitle') . '" id="' . $this->get_field_id('wp_table_subtitle') . '" >'; echo '<label for="' . $this->get_field_id('wp_column') . '">Spalten: </label>'; echo '<input class="widefat" type="text" value="' . $instance['wp_column'] .'" name="' . $this->get_field_name('wp_column') . '" id="' . $this->get_field_id('wp_column') . '" >'; echo '<label for="' . $this->get_field_id('wp_line') . '">Zeilen pro Spalte: </label>'; echo '<input class="widefat" type="text" value="' . $instance['wp_line'] .'" name="' . $this->get_field_name('wp_line') . '" id="' . $this->get_field_id('wp_line') . '" >'; if($instance['wp_column'] == '' || $instance['wp_line'] == '') { echo 'Bitte beide Felder ausfüllen!'; } else { $spalten = $instance['wp_column']; $zeilen = $instance['wp_line']; for ($i = 1; $i <= $zeilen; $i++) { echo "<label style='display: block; margin-top: 10px;'>Zeile {$i}</label>"; for ($j = 1; $j <= $spalten; $j++) { $instSpecLine = $instance["spec_line_{$i}_{$j}"]; echo "<input class='widefat' type='text' value='{$instSpecLine}' name='{$instSpecLine}' id='{$instSpecLine}' placeholder='Spalte {$j}' >"; } $j = 0; } } } function update($new_instance, $old_instance) { $instance['wp_table_title'] = $new_instance['wp_table_title']; $instance['wp_table_subtitle'] = $new_instance['wp_table_subtitle']; $instance['wp_line'] = $new_instance['wp_line']; $instance['wp_column'] = $new_instance['wp_column']; $spalten = $instance['wp_column']; $zeilen = $instance['wp_line']; for ($i = 1; $i <= $zeilen; $i++) { for ($j = 1; $j <= $spalten; $j++) { $instSpecLine = $instance["spec_line_{$i}_{$j}"]; $instance[$instSpecLine] = $new_instance[$instSpecLine]; } } return $instance; } function widget($args, $instance) { /*Ausgabe im Frontend*/ echo $args['before_widget'];
echo $instance['wp_table_title']; echo $instance['wp_table_subtitle']; $spalten = $instance['wp_column']; $zeilen = $instance['wp_line']; for ($i = 1; $i <= $zeilen; $i++) { for ($j = 1; $j <= $spalten; $j++) { $instSpecLine = $instance["spec_line_{$i}_{$j}"]; echo $instSpecLine; } } echo $args['after_widget']; }} add_action('widgets_init', function() { register_widget('table_widget');});