Beiträge von Sparkley

    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');});

    Vielen Dank!
    Das ganze läuft jetzt mit folgendem Code, falls jemand nochmal auf den Thread stoßen sollte:

    Hast du eine Ahnung, warum das jetzt funktioniert und vorher nicht?

    Achso und bevor ich es vergesse:
    Mein Code aus dem obersten Beitrag hat die Original URL in das Inputfeld geladen, wenn man zunächst in der Media Library auf Datei URL geklickt hat und dann auf "In Beitrag einfügen". Komisch, aber so sollte es ja auch nicht "funktionieren".

    Vielen Dank Daniel für Deine Hilfe!

    Vielen Dank für deine Antwort! Die Klasse im Formular gibt es:

    PHP
    ...[COLOR=#0000BB][FONT=monospace]ame[/FONT][/COLOR][COLOR=#007700][FONT=monospace]( [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'image' [/FONT][/COLOR][COLOR=#007700][FONT=monospace]); [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]?>[/FONT][/COLOR][COLOR=#000000][FONT=monospace]" id="[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]<?php [/FONT][/COLOR][COLOR=#007700][FONT=monospace]echo [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]$this[/FONT][/COLOR][COLOR=#007700][FONT=monospace]->[/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]get_field_id[/FONT][/COLOR][COLOR=#007700][FONT=monospace]( [/FONT][/COLOR][COLOR=#DD0000][FONT=monospace]'image' [/FONT][/COLOR][COLOR=#007700][FONT=monospace]); [/FONT][/COLOR][COLOR=#0000BB][FONT=monospace]?>[/FONT][/COLOR][COLOR=#000000][FONT=monospace]" class="widefat [B]image-url[/B]" type="text" ...[/FONT][/COLOR]


    Der Part auf den du dich beziehst funktioniert sowieso nicht, weil ich ja nicht den Inhalt der Variable ausgebe, sondern den String. Wollte so nur testen, ob der part ausgeführt wird, wenn ich mit der Media Library interagiere. Sorry, hätte ich erwähnen sollen.
    Mein Problem ist, dass wenn ich mit console.log versuche die Variable imgurl ausgeben möchte, undefined kommt und ich mir nicht erklären kann, warum.

    Also das Problem ist, dass ich die URL nicht in die Variable bekomme, wenn ich bei der Media Library auf "In Beitrag einfügen" klicke. Bezieht sich also auf diese Zeile:

    Code
    [COLOR=#333333]var imgurl = $('img',html).attr('src');[/COLOR]

    Viele Grüße

    Hallo liebe Community,
    leider habe ich ein Problem bei der Programmierung eines Widgets und habe auch nach langer Recherche nichts finden können. Soweit funktionieren die Textfelder des Widgets, allerdings gibt es ein Problem bei der Übergabe der Bild Url. Diese wird nicht in das Input Feld geladen, wenn man beim Bild in der "Media Library" auf "in den Beitrag einfügen" klickt. Ich konnte das Problem soweit lokalisieren, dass die Variable "imgurl" in meinem Javascript Code leer ist. Weiß da allerdings nicht mehr weiter.

    Schon mal vielen Dank für eure Mühen

    Sparkley

    Mein PHP Code

    PHP
    class home_image_box extends WP_Widget {    public function add_admin_scripts() {    //media btn    wp_enqueue_script('media-upload');    wp_enqueue_script('thickbox');    wp_enqueue_script('widgetImage', get_stylesheet_directory_uri() . '/js/widgetImage.js', array( 'jquery' )) ;}    public function add_admin_styles() {    wp_enqueue_style('thickbox');}      function __construct() {    $widget_ops = array(      'classname' => 'home_image_box',      'description' => 'Bildbox Startseite',    );    parent:: __construct(false, $name = __('Bildbox Startseite'), $widget_ops);        add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts' ) );    add_action( 'admin_enqueue_styles', array( $this, 'add_admin_styles' ) );  }   function form($instance) { /*Ausgabe im Backend*/    echo '<h2>Beispiel</h2>';    echo '<p>Hier kann ich etwas dem Benutzer mitteilen!</p>';
        echo '<label for="' . $this->get_field_id('home_image_box_title') . '">Titel: </label>';    echo '<input class="widefat" type="text" value="' . $instance['home_image_box_title'] .'" name="' . $this->get_field_name('home_image_box_title') . '" id="' . $this->get_field_id('home_image_box_title') . '" >';  ?>
        <label for="<?php echo $this->get_field_name( 'image' ); ?>"><?php _e( 'Bild URL:' ); ?></label>    <input name="<?php echo $this->get_field_name( 'image' ); ?>" id="<?php echo $this->get_field_id( 'image' ); ?>" class="widefat image-url" type="text" size="36"  value="<?php echo esc_url( $image ); ?>" />    <input class="upload_image_button" type="button" value="Upload Image" />    <?php    echo '<label for="' . $this->get_field_id('home_image_box_link') . '">Link: </label>';    echo '<input class="widefat" type="text" value="' . $instance['home_image_box_link'] .'" name="' . $this->get_field_name('home_image_box_link') . '" id="' . $this->get_field_id('home_image_box_link') . '" >';          echo '<label for="' . $this->get_field_id('home_image_box_link_text') . '">Linktext: </label>';    echo '<input class="widefat" type="text" value="' . $instance['home_image_box_link_text'] .'" name="' . $this->get_field_name('home_image_box_link_text') . '" id="' . $this->get_field_id('home_image_box_link_text') . '" >';          echo '<label for="' . $this->get_field_id('home_image_box_text') . '">Text: </label>';    echo '<input class="widefat" type="text" value="' . $instance['home_image_box_text'] .'" name="' . $this->get_field_name('home_image_box_text') . '" id="' . $this->get_field_id('home_image_box_text') . '" >';  }   function update($new_instance, $old_instance) {    $instance['image_uri'] = strip_tags( $new_instance['image_uri'] );    $instance['home_image_box_title'] = $new_instance['home_image_box_title'];    $instance['home_image_box_link'] = $new_instance['home_image_box_link'];    $instance['home_image_box_link_text'] = $new_instance['home_image_box_link_text'];    $instance['home_image_box_text'] = $new_instance['home_image_box_text'];    return $instance;  }   function widget($args, $instance) { /*Ausgabe im Frontend*/    echo $args['before_widget']; ?>      <img src="<?php echo esc_url($instance['image_uri']); ?>" />    <?php echo $args['after_widget'];  }} add_action('widgets_init', function() {  register_widget('upper_bar_widget');  register_widget('home_image_box');});

    Mein JS Code